List give a count of zero when it have 1 element
Like this photo show
the list is showing Count zero when is clear that it have 1 element in it. I have the script execution order set before the script that it call the list count.
Could you provide your code? It's hard to diagnose without seeing how you are trying to do it.
Here is where I add the elements to the list
Pellet ghostHouse1 = GameObject.Find("Pellets/23_ghost_house_empty1(Clone)").GetComponent<Pellet>();
Pellet ghostHouse2 = GameObject.Find("Pellets/24_ghost_house_empty2(Clone)").GetComponent<Pellet>();
Pellet ghostHouse3 = GameObject.Find("Pellets/25_ghost_house_empty3(Clone)").GetComponent<Pellet>();
Pellet ghostHouseNeighbour1 = GameObject.Find("Pellets/26_ghost_house_neighbour1_empty(Clone)").GetComponent<Pellet>();
Pellet ghostHouseNeighbour2 = GameObject.Find("Pellets/27_ghost_house_neighbour2_empty(Clone)").GetComponent<Pellet>();
Pellet ghostHouseNeighbour3 = GameObject.Find("Pellets/28_ghost_house_neighbour3_empty(Clone)").GetComponent<Pellet>();
if(this.name == "23_ghost_house_empty1(Clone)")
{
if(ghostHouse1 != null && ghostHouseNeighbour1 != null)
{
neighbours.Clear();
neighbours.Add(ghostHouseNeighbour1);
validDirections.Clear();
Vector2 vec = ghostHouseNeighbour1.transform.localPosition - transform.localPosition;
vec = vec.normalized;
validDirections.Add(vec);
}
}
if (this.name == "24_ghost_house_empty2(Clone)")
{
if (ghostHouse2 != null && ghostHouseNeighbour2 != null)
{
neighbours.Clear();
neighbours.Add(ghostHouseNeighbour2);
validDirections.Clear();
Vector2 vec = ghostHouseNeighbour2.transform.localPosition - transform.localPosition;
vec = vec.normalized;
validDirections.Add(vec);
}
}
if (this.name == "25_ghost_house_empty3(Clone)")
{
if (ghostHouse3 != null && ghostHouseNeighbour3 != null)
{
neighbours.Clear();
neighbours.Add(ghostHouseNeighbour3);
validDirections.Clear();
Vector2 vec = ghostHouseNeighbour3.transform.localPosition - transform.localPosition;
vec = vec.normalized;
validDirections.Add(vec);
}
}
and here is where I need the list element
if(isInGhostHouse)
{
direction = Vector2.up;
targetPellet = currentPellet.neighbours[0];
Debug.Log(currentPellet.neighbours[0]);
startingPosition = currentPellet;
//Debug.Log(startingPosition);
}
else
{
direction = Vector2.left;
targetPellet = ChoseNextPellet();
}
but as seen in the picture it have 1 element whule Debug.Log say Count = 0
Hello.
First, use of GameObject.Find() spends a los of resources. try to use more often GameObject.FindObjectWithTag().
Second, if you are sure the array its suposed to read have more than 0 elements; Or you are not reading the correct list, or you change the number of the elements of the list before reading.
as it show in inspector it have 1 element and I don't change any elements in the list between adding and reading. how can I not read the correct list ?
"how can I not read the correct list ? "
I mean maybe you make a mistake writing the code and not selected the correct object or list. Or list is not genereted when you read it.
Answer by DaresFireson · May 10, 2019 at 07:31 AM
I am creating my map from a file and I moved the map creation in the Awake function and it work. it seems that script execution order didn't help this time.