- Home /
GameObject.Find() type casting question
I have the following situation that I'm trying to figure out.
Tile NorthTile = GameObject.Find("Tile_A2");
If I do this, it gives me a compiling error that I cannot cast a GameObject into a Tile. So I tried this
Tile NorthTile = (Tile)GameObject.Find("Tile_A2");
I still get the same exact error when I do this. Is there a different way to do type casting to get the results I need?
Answer by Mortennobel · Apr 19, 2011 at 07:51 PM
It's because GameObject.Find returns a game object.
What you are looking for is the Tile component on the returned GameObject-object
The code should be something like (in JavaScript)
var NorthTile = GameObject.Find("Tile_A2").GetComponent.<Tile>();
Or C#:
Tile NorthTile = GameObject.Find("Tile_A2").GetComponent<Tile>();
JS is var NorthTile = GameObject.Find("Tile_A2").GetComponent.();
Ah yes. UnityScript is not my preferred language :-) I'll fix the answer :-) Thanks!
Your answer
Follow this Question
Related Questions
Problem with type casting? 1 Answer
Conversion of GameObject with Component to Specific Type 0 Answers
How can I use a variable with getcomponent<>()? 2 Answers
type not found 1 Answer