I have two scripts attached to a gameobject, one of them creates a list of custom objects and then the second script attached to the same gameobject needs to access this list to iterate through it in order to know which objects to display.
I have written the following code example to show the problem I am having.
using UnityEngine;
using System.Collections.Generic;
public class TestScript : MonoBehaviour {
public List<int> myList;
void Start () {
myList.Add (7);
myList.Add (8);
myList.Add (9);
}
public List<int> GetList(){
return myList;
}
}
using UnityEngine;
using System.Collections;
public class TestScript2 : MonoBehaviour {
private GameObject myGameObject;
void Start () {
myGameObject = this.gameObject;
TestScript myTestScript1 = myGameObject.GetComponent<TestScript> ();
foreach (int item in myTestScript1.GetList()){
print (item);
}
}
}
I’m not sure why I cannot access the list. I have tried doing it directly as a public variable but still have the same issue.
When I run it in my actual code the objects in the list are gameobjects with textmesh renderers and custom variables. And I also get “Object reference not set to an instance of an Object” errors when I try to access the list of these objects.
I thought it might be because I need to ensure the list creation script is executed before the second script trying to access the list, so I tried adding it to the script execution order settings before with it ordered before the second accessing script. However this didnt’ stop the error.
In your example you never instantiate the List of type int.
You should either instantiate the field in line or in the Start method.
// Old script
using UnityEngine;
using System.Collections.Generic;
public class TestScript : MonoBehaviour {
public List<int> myList;
void Start () {
myList.Add (7);
myList.Add (8);
myList.Add (9);
}
public List<int> GetList(){
return myList;
}
}
// New script
using UnityEngine;
using System.Collections.Generic;
public class TestScript : MonoBehaviour {
public List<int> myList = new List<int>(); // instantiate here
void Start () {
// or instantiate the list here
myList = new List<int>();
myList.Add (7);
myList.Add (8);
myList.Add (9);
}
public List<int> GetList(){
return myList;
}
}
Be cause you said:
I have tried doing it directly as a public variable but still have the same issue
I also assumed you were not setting the List of int in the inspector.
To have access to methods of object, first you must get instance of this object. For example I have ScriptA and ScriptB:
using UnityEngine;
using System.Collections.Generic;
public class ScriptA : MonoBehaviour {
public string methodA() {
return "Method A";
}
}
ScriptB:
using UnityEngine;
using System.Collections.Generic;
public class ScriptB : MonoBehaviour {
public ScriptA instanceScriptA;
void Start() {
Debug.Log(instanceScriptA.methodA());
}
}
So you must attach some GameObject with ScriptA component to GameObject with ScriptB component.
Second method is to store static singleton instance of your script to have access.
ScriptA:
using UnityEngine;
using System.Collections.Generic;
public class ScriptA : MonoBehaviour {
public static ScriptA instance;
void Awake() {
if (instance == null)
instance = this;
}
public string methodA() {
return "Method A";
}
}
ScriptB:
using UnityEngine;
using System.Collections.Generic;
public class ScriptB : MonoBehaviour {
void Start() {
Debug.Log(ScriptA.instance.methodA());
}
}