- Home /
How Do I Access and Change Items in a List on Another Script?
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.
Around Line 35 is where I want to put the code to access the info ... I want to be able to access the info by mouse clicking on each unit/object which will then display info on a GUI.
I'm still working on how to get the index number of the list from just a mouse click so that I can access the remainder of the information about that unit/object by utilizing the index of that item...but that is really a separate question unless someone feels compelled to answer that here (which I would greatly be thankful for).
Thanks in advance for any help.
Answer by TheSOULDev · Sep 02, 2017 at 12:51 AM
Simply make the list static (then you can access it by ClassName.ListName
), or initialize an instance of the script containing your list into the script you want to access it from. It is no different than editing a value.
Thank you, I tried making it a static but I must have done something incorrectly...I will attempt this again per your instructions to see if it fits the bill.
I appreciate your help.
I worked on this all day yesterday and I swear I thought I tried everything including making the list static (I must have been trying to access it wrong or had slightly incorrect syntax or something)...but your suggestion seems to work perfectly today.
...and I'm not kidding when I said I worked on it all day. Sometimes a new day and a good suggestion can make a world of difference.
I'm continuing to evaluate some of the other items below but right now I'm pretty happy.
You should keep in $$anonymous$$d that while static is really a one-hit wonder solution, you should use static variables sparingly. You should use static variables for something that only one instance can exist of, something that is constantly being accessed by plenty of other scripts and something that is possibly not a security issue. If you need to access your list in only one script without any indication you'll ever call it from another script, then initializing and instance of the mother script of the list is a much better idea.
Answer by MaxGuernseyIII · Sep 02, 2017 at 01:01 AM
@TheSOULDEv is correct. You can also factor the list out into its own class, neither part of SpawnAll nor Movement. Then you can make that class a singleton (which you can look up in about a million places). Both those solutions amount to having a global variable in your code.
If you do not want global variables, you can create a public field of type SpawnAll on your Movement behavior and access members of that field. Then, in the inspector, you can drag the GameObject with the SpawnAll script into the editor field that creates.
Thank you too...I will look at both your suggestions to see if they match my needs.
I really appreciate that you took the time to help me.
I'm still looking at both your suggestions. I'm especially interested in your "singleton" comment and I'm going to try to track down a couple of those "million places", if for nothing else, just to learn something that might help me in the future.
I'm not totally sure if I fully understand your second paragraph but I will also try to explore that before I move forward.
Thank you again for your help.
Your answer
Follow this Question
Related Questions
How do I make a list of references to a variable? 2 Answers
How to access List<> from other script? 1 Answer
Last time I'll ask for help on this lol 2 Answers
how can i check if there is a blanc spot in my list 3 Answers
Multiple Cars not working 1 Answer