- Home /
Question by
SmugMonster · Dec 10, 2019 at 10:17 PM ·
listsadd
Problem adding to a list (count always returning 0)
Hi folks. Got a head-scratcher here. This is part of a fairly huge set of scripts, but here's what it boils down to:
I'm initializing a list like this:
public List<Boardtile> possibleMoveTiles = new List<Boardtile>();
Then, awhile later, I'm adding to it like this:
if(Physics.Raycast(x.target_pos, transform.TransformDirection(Vector3.down), out hit, 25f, LayerMask.GetMask("ChessPlane")))
{
x.hitnode = hit.transform.GetComponent<Boardtile>();
if(x.hitnode.occupied_AI == true)
{
x.occ_code = x.hitnode.occupant_AI_code;
if(!x.occ_code.isWhite && !x.occ_code.captured)
{
possibleMoveTiles.Add(x.hitnode);
Debug.Log("Possible move tile count: "+possibleMoveTiles.Count);
}
}
}
Where x.hitnode is a Boardtile script component being returned by a raycast. It's successfully picking up the Boardtile component, so x.hitnode exists and is the right thing, it's just not successfully adding to it.
possibleMoveTiles.Count is returning 0, seems like it doesn't want to add to the list and I can't quite figure out why.
Any help would be much appreciated.
EDIT: I also tried this:
possibleMoveTiles.Add(x.hitnode);
x.hitnode = new Boardtile();
But that just gave me an error "You are trying to create a MonoBehaviour using the new keyword".
Comment