Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instatiate() confusion

Discussion in 'Scripting' started by RichardSmith, Nov 10, 2010.

  1. RichardSmith

    RichardSmith

    Joined:
    Oct 19, 2010
    Posts:
    27
    Hi there experts. Please enjoy a wry smile at my newbie confusion. :D

    I'm doing some early tests on Unity3.0 iOS with C#. I have managed to create a builtin Array of Objects and populate it with Objects spawned from a prefab in the assets folder at runtime using the return results from Instantiate() in a for loop on Start(). The prefabs spawn correctly into the game world with random positions.

    I'd now like to manipulate those Objects using Rotate() Translate() within the Update() etc...

    myArray[0].transform.Translate(1.0f,0.0f,0.0f);


    Q1. The Objects referenced by the array don't seem to allow for this. Do I need to cast differently or use getCompenent or something similar?

    Q2. Why does Instantiate() return an Object data type, not a GameObject? I thought Instantiate cloned asset prefabs, and a prefab was a GameObject.


    Many thanks for any advice!

    Richard
     
  2. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
  3. RichardSmith

    RichardSmith

    Joined:
    Oct 19, 2010
    Posts:
    27
    Hi ivkoni,

    I was originally creating an array of Objects as Instantiate returned data type Object.

    I have just tried creating an array of GameObjects, and explicitly casting the return result of Instantiate() to type (GameObject).

    I can now access the tranform functions. Hooray!

    Thanks for your insanely fast response.

    Richard