Can't reach information stored in a class list,Can't reach informations store in a list
Hi Everyone !
It's a few month that i'm learning coding in unity for a puzzle platformer.
For a few days i'm stock on a script that have for goal to simplify the level designe of puzzle rooms.
The idea is following:
In a puzzle room we have some activators (for ex. levers,pressure plates, etc) and effectors (for ex. door, bridges, moving platforms, etc).
Each have an specific ID express in int so that the script can compare wich activator is related with wich effector so I can do stuff like if(activator.id == effector.id){ do stuff }
For that the script uses a class list that store all the effector ID and GameObjects related to it. The activtor ID is stored in a temp int when the player collide with it.
All works fine until I try to acces the differents informations stored in the List (like the id int for ex.)
I tried to use myList.Find() or even myList.Exist() but it don't show up the variables stored in the class. I clearly do something wrong but can't find what.
So here's my script :
protected int actualActivator;
protected GameObject activator;
public GameObject[] effectors;
public List<effectorID> effectorIDList = new List<effectorID>();
public class effectorID
{
public GameObject effector_in_list;
public int ID_in_list;
public effectorID (GameObject newEffector, int newID)
{
effector_in_list = newEffector;
ID_in_list = newID;
}
}
private void Awake()
{
foreach (GameObject effector in effectors)
{
int thisID = effector.GetComponent<Effector>().EffectorID;
effectorIDList.Add(new effectorID (effector, thisID));
}
}
Thanks in advance, and have a nice (sunny) day !
What is "myList"? Is that the gameObject where the above script is attached to? Could you post the complete class which extends from $$anonymous$$onoBehavior? From where you are trying to call Find()?
Answer by Chris333 · Apr 05, 2019 at 02:09 PM
I guess your above code is a class which extends from MonoBehavior and is a component on a gameObject in the scene. So to access the specific information you are trying to get you need to call the following code.
List<effectorID> newEffectorIDList = GameObject.Find("TheNameOfYourGameObjectInScene").GetComponent<ClassNameOfTheAttachedScript>().effectorIDList;
Hint: Calling Find can be very expensive because it traverse the complete scene tree to find your gameObject. Keep that in mind for later improvements.
Your answer
