- Home /
Accessing GameObjects in my List
I get a NullReferenceException error in my last line of code. I'm assuming I'm trying to access the bool in the other script incorrectly, but I don't know how else to do it.
public class GoList : MonoBehaviour {
public GameObject[] enemyList;
public int enemyListLength;
// Use this for initialization
void Awake () {
enemyList = GameObject.FindGameObjectsWithTag ("Enemy");
enemyListLength = enemyList.Length;
List<GameObject> GObj = new List<GameObject> ();
for (int i = 0; i < enemyListLength; i++){
GObj.Add (enemyList[i]);
}
GObj.Sort (SortMethod);
foreach (GameObject go in GObj) {
print (go.name);
}
GObj[0].GetComponent<PlayerTurn>().goFirst = true;
}
Answer by zharik86 · Sep 03, 2014 at 08:33 PM
Here two possible decisions. The first, check that on a scene there was at least one object with the "Enemy" tag. If objects are available, most likely, there is no "PlayerTurn" component. And what at you a sorting method. Check script operation without sorting. If works, the problem in sorting means.
I'm a dunce and put the script on the wrong GameObject. Thank you for the help!
Your answer
Follow this Question
Related Questions
Add and destroy instantiated gameobject in linkedlist C# 1 Answer
How to run a function in a GO that have DontDestroyOnLoad 1 Answer
What is the best way to instatiate an array of connected GameObjects? 0 Answers
Subtracting the position of transform from the position of Game Objects in a list. 1 Answer