Find gameobjects with name and store in a array

Hello.
How can I find game object with a certain name and store them in a array?

I wrote following script, but there is a error “Cannot convert ‘UnityEngine.GameObject’ to ‘UnityEngine.GameObject’.”

//javascript

private var targets : GameObject[];

function Start () {
	targets = this.gameObject.transform.Find("target").gameObject;
}

Hi , you can set tag for gameobjects and find them by tag

GameObject[] objects =  GameObject.FindGameObjectsWithTag("TAG");

import System.Linq;

   ...

   targets = GetComponentsInChildren.<Transform>().Where(function(c) c.name == "target").ToArray();

If you have the names you can use GameObject.Find(“name”); to give you the game object. However, this will only give you ONE game object, not an array of game objects.

What you’re currently doing with your script is grabbing A GameObject and attempting to set an array of GameObjects to a single GameObject. To add that game object to your array, you can use

targets[0] = GameObject.Find(“name”);

This would make the first item in the array the GameObject that you found. However, you would then have to keep track to not overwrite that object. A list may be more useful to you.

Or perhaps you are trying to find a group of objects that all have the same name and setting that value to an array? Honestly I’m not sure what it is you want your code to do, perhaps a little more explanation would be useful.

FindObjectsOfType() is probably what you want:

http://docs.unity3d.com/Documentation/ScriptReference/Object.FindObjectsOfType.html

You can list all the objects in the scene to an array:

var gameObjs : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];

then search that array by gameobject.name
it’s not fast enogh for a pro streaming code but it can work.