[n00b] Referencing an object created via Instantiate()

Hi All,

Again I have a questions for something which I’m sure is going to be a relatively easy answer but for some reason I’m struggling to find the answer for it. Basically what I’m looking at doing is referencing an object created using the Instantiate command.

So an example would be (though this doesn’t work):-

GameObject newGroundTile = Instantiate( GameObject.Find( "GroundTile" ), new Vector3( 5f, 0, 5f ), Quaternion.identity );
newGroundTile.transform.position = new Vector3( 0, 0, 0 );

Now in this situation I’m aware that I can just set the position when creating it, but this is just an example to show what I’m trying to do. I may want to do some additional effects / rotations / what ever on it once it’s created depending on other factors. When trying to do whats above I get

I try changing over to the type over to Object rather than game object and I no longer get the error, but I can’t reference any of the objects properties to actually change it. (eg newGroundTile.transform.position). Now I’m not sure if I should be using this to reference the object somehow, or if I should be finding the reference to the object another way all together… If anyone is able to point me into the right directions it would be appreciated!

Stevil

When you use Instantiate in C#, the return type is Object. You’ll need to put as GameObject at the back to make it a GameObject. So in your case:

GameObject newGroundTile = Instantiate( GameObject.Find( "GroundTile" ), new Vector3( 5f, 0, 5f ), Quaternion.identity ) as GameObject;

Also, I wouldn’t recommend using GameObject.Find. I’d say store the game object you want to create a copy of as a variable (by making the variable public and dragging and dropping it into the script via the inspector) and use that when you instantiate.

Never mind, I resolved it. Could jsut cast type it like so:-

That works!

Yet again another problem I solved myself before anyone got a chance to help… hopefully my bumblings are going to help other people with the same issues as me!

Spoke too soon, someone did reply. Thanks for that, I like the way you did it better as it’s easier to read. Appreciate it!