- Home /
GameObejct.Find using string variable for name
usually you'd type
gameObject.Find("someObject")
to find that object, but in my game I'm using
gameObject.Find(string1)
where string one is the name of a game object. Like this:
string1 = "First Person Controller"; string2 = "FPSWalker";
gameObject.Find(string1).transform.GetComponent(string2).some Function();
however I get the following error:
Assets/Scripts/TerrainScript.js(34,48): BCE0017: The best overload for the method 'UnityEngine.GameObject.Find(String)' is not compatible with the argument list '(char)'.
it doesn't make any sense to me because I'm just replacing a string with a string variable, why does it not work?!
It would probably help if you pasted the actual code you're using, not an example of what it's like.
Answer by duck · Apr 28, 2010 at 07:13 AM
Well, in the code you pasted, you have a lowercase g for gameObject when it should be GameObject, and you have a space in the "some Function()" name. Neither of these is likely to be your problem though.
It's most likely an error in some part of the code that you haven't pasted.
You'll convert a string to a 'char' if you use square bracket access on the string at some point, so I guess you're doing this by accident earlier in the script. Eg:
var myString = "foo";
var obj = GameObject.Find(myString[1]);
I tried it with both a GameObject and lowercase gameObject and I get the same thing. Ah, you're right, I was making string1 = whodo[0] which I thought I had assigned to be an array but it was actually set as a string thus giving us the 'char' error! excellent work $$anonymous$$r. Duck!
Your answer
Follow this Question
Related Questions
NullReferenceException when transform.Find(string) STILL 1 Answer
String to Var 1 Answer
Variable from String 0 Answers
Access a GameObject through it's tag, using a string. 1 Answer