- Home /
Access list from other script
I have a script with a list. But how do I check that list from another script?
Comment
Answer by HCIC · Jan 09, 2019 at 01:20 PM
Assuming you have ScriptA and ScriptB, and the list is in scriptB, you can either:
Make a static linked instance of ScriptB and access it through the class identifier
public class ScriptB{
public ScriptB instance;
private List list;
public void Start(){
instance = this;
}
public static List GetList(){
if(instance) return list;
return null;
}
}
public class ScriptA{
public void foo(){
List list = ScriptB.GetList();
}
}
Or you can get it through an assigned object in the inspector. This assumes that ScriptA inherits from MonoBehaviour:
public class ScriptA : MonoBehaviour{
public ScriptB scriptB;
public void foo(){
List list = scriptB.GetList();
}
}
You'll need to drag & drop the script into the public variable slot - this only works if you have an instance of the ScriptB, aka it is a component of a GameObject.