- Home /
how do i use GameObject.FindGameObjectsWithTag() on lists
var spawnPoints:GameObject[];
function Start () {
spawnPoints=GameObject.FindGameObjectsWithTag("spawn");
}
this works fine but when i change spawnPoints to a list i get this error"Cannot Convert UnityEngine.GameObject[]to Systems.Generic.List..
Answer by syclamoth · Sep 09, 2013 at 09:20 AM
Remember that while they can often be used in the same way, an array and a List are fundamentally different things. You can't just assign an array to a list reference and expect them to work.
However, the List API comes with a function for doing exactly what you want to do here!
Just use
spawnPoints.AddRange(/*whatever you need to add*/);
and you are good to go.
Remeber that spawnPoints needs to have been created before you can add stuff to it- you will need to allocate a new List object or you will get a Null Reference Exception when you try to add new items to it.
You can also pass an array returned from FindGameObjectsWithTag to the List constructor:
spawnPoints = new List.<GameObject>(GameObject.FindGameObjectsWithTag("spawn"));
ArkaneX - this was spot on for something I was working on ! Thanks for this. One note - I think you have an extra "." in the code above though. $$anonymous$$aybe it was changed, but I think you don't need the dot in this part between "List" and "<"
Holy crap I can't get the triangular brackets to print! lol...
Your answer
Follow this Question
Related Questions
Logical Error In Spawn Counter (Javascript) 2 Answers
2D Spawn with fixed random locations 0 Answers
How to Spawn a Gameobject According to a Timer? 2 Answers
Spawning Creatures 0 Answers