- Home /
How to access the return value of GameObject.FindGameObjectsWithTag() ?
The Documentation says the function returns a "list" of GameObjects without saying if this means they are returned as a collection or array, but both accessing methods don't work:
var places = GameObject.FindGameObjectsWithTag ("place");
var i = Random.Range(0,places.length);
myRandomPlace = places[i];
// nor: myRandomPlace = places.item(i);
The Debug window says the return type of the function is a GameObject itself, do I have to traverse it?
Hi $$anonymous$$. You might want to rewrite your question to remove the inaccuracies (i.e. that places[i] doesn't work). Some people might not even know to try places[i], so it's still a useful question.
Answer by abdullah.ahmed · Nov 09, 2009 at 06:39 PM
From the documentation it looks to be just a generic array of game objects.
You'd have to traverse it using a for loop in order to find the specific object you are looking for.
Check out the following article which details the different types of collections available in JS and C# for Unity 3d. Unity Coding: Arrays, Hashtables and Dictionaries explained
Answer by Thomas · Nov 09, 2009 at 08:52 PM
I've solved my problem, it was actually not this part of the script but a later GetComponent("ScriptName") Call, which returned Null and raised a NullReferenceExpection. I thought it happened when I assigned the GameObject to the variable, but it didn't. Referencing the array like I have in the above example does work.
Thank you Achmed for the link to the article, brings some light to the topic.