GameObject.Find Problem with Array


In "myFirstScript" i fill an array with values. The array is defined like this:

static var COLLECTED_ITEMS = new Array ();

The filled values are the names of Game Objects in my Game. And in another script I want to access this array (in this case the first element):

myVar = GameObject.Find(myFirstScript.COLLECTED_ITEMS[0]).GetComponent("Info_Script");

The script "Info_Script" is attached to the Game Object with the name that equals the first array element. But it gives me an "UnityEngine.GameObject.GetComponent (System.String type)" error? Can you help me with this problem?

Thx a lot!

I was not able to reproduce your error, armed with only the code and information you have provided. However, you may have better luck, if you use better coding practices. Generic Lists should now be used instead of Unity's Arrays, for a number of reasons, but the most important for your case is that they have type safety, which can help you catch errors, especially with the use of #pragma strict at the top of your code. GetComponent with a string is also to be avoided unless you really need it; strings really are not helpful for generating compile-time errors. Also, you really should check out Duck's second answer here, which will give you some standardized style tips.

static var collectedItems = List.<String>();


myVar = GameObject.Find(MyFirstScript.collectedItems[0]).GetComponent.<Info>();