I create a list of items in this script…works fine to create units which are small flat cylinders that look like buttons.
public class SpawnAll : MonoBehaviour {
public static int friendCounter01 = 0;
public GameObject CombatUnit;
public List<FriendlyCombatUnit> friendlyCombat = new List<FriendlyCombatUnit> ();
void Awake () {
for (int i = 0; i < 5; i++) {
SpawnFriendlyCombatUnit ();
}
}
void SpawnFriendlyCombatUnit(){
Instantiate (CombatUnit);
CombatUnit.transform.position = new Vector3 (Random.Range (5f, 25f), .5f, Random.Range (5f, 25f));
string tempName = (friendCounter01 * 111).ToString (); // Placeholder
int tempOne = (int) Random.Range(95f,106f);
int tempTwo = (int)Random.Range (45f, 56f);
int tempThree = (int)Random.Range (20f, 31f);
int tempNum = friendCounter01;
friendlyCombat.Add(new FriendlyCombatUnit(tempNum, tempName, tempOne, tempTwo, tempThree));
friendCounter01++;
Debug.Log (friendCounter01);
}
}
public class FriendlyCombatUnit{
public int unitID { get; set; }
public string unitName { get; set; }
public int unitStrength { get; set; }
public float unitOrganization { get; set; }
public int unitExperience { get; set; }
public FriendlyCombatUnit(int i, string n, int s, float o, int e)
{
unitID = i;
unitName = n;
unitStrength = s;
unitOrganization = o;
unitExperience = e;
}
}
I’d like to access the List information from another script. I’ve tried about everything I know of and researched about everything I can think of online but cannot find anything on point.
public class Movement : MonoBehaviour {
private bool isSelected = false;
public NavMeshAgent agent;
SpawnAll s1;
void Awake(){
s1 = GetComponent<SpawnAll> ();
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
Collider col = hit.collider;
if (col.gameObject.tag == "Friendly") /// /// /// Begin Tag/Friendly section //////////////////////////////////////////////
{
GameObject[] tempGo; /// This "tempGo" section works to turn all units Blue and to change "isSelected" to false //////////////////////////////////////////
tempGo = GameObject.FindGameObjectsWithTag ("Friendly");
foreach (GameObject thing in tempGo) {
thing.GetComponent<Renderer> ().material.color = Color.blue;
isSelected = false;
} /// /// /// /////////////////////////////////////////////////////////// End "tempGo" Section ///////////////////////////////////////////////////////////
isSelected = true;
agent = col.gameObject.GetComponent<NavMeshAgent> ();
col.gameObject.GetComponent<Renderer>().material.color = Color.cyan;
} /// /// /// End tag/Friendly section ///////////////////////////////////////////////////////////////////////////////////////
if (col.gameObject.name == "Terrain" && isSelected == true){
//Debug.Log (hit.point);
}
if (col.gameObject.name == "Terrain") {
//Debug.Log (hit.point);
}
}
}
if (Input.GetMouseButtonDown (1) && isSelected == true) {
Ray ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit1;
if (Physics.Raycast (ray1, out hit1)) {
Collider col = hit1.collider;
agent.SetDestination (hit1.point);
isSelected = false;
gameObject.GetComponent<Renderer>().material.color = Color.blue;
}
}
}/// /// /// Last bracket of "Update" method.
}
Anyway, I’m stumped as to how to access the List in the script SpawnAll from another script.