Search Unity

How do I use Instantiate to create unique instances of my prefab?

Discussion in 'Scripting' started by TheSirShade, Jan 15, 2019.

  1. TheSirShade

    TheSirShade

    Joined:
    Dec 28, 2018
    Posts:
    2
    Hi All,

    I'm trying to understand instances and am creating a grid out of a prefab square. I am changing the color of the instances of the prefabs that fall on the edge of the grid. However, it appears that each time my instance is instantiated that it has the exact same instanceID as the one just placed. Can anyone tell me how I get a unique instanceID?
    upload_2019-1-15_1-32-48.png

    Console Log:
    upload_2019-1-15_1-33-41.png

    Any help will be much appreciated!
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    You need to use the output of Instantiate(), that is the handle to your new GameObject.
     
  3. You're changing the prefab itself and you're reding the prefab's instanceid.
    Unfortunately you didn't post your code properly (in text format inside code tags instead of screenshot).
    So you need to store your instance in a variable in the line where you use the Instantiate command. Look at the Instantiate command's manual scripting API it contains an example how to proceed.
     
  4. TheSirShade

    TheSirShade

    Joined:
    Dec 28, 2018
    Posts:
    2
    @PGJ and @Lurking-Ninja Thank you both for your response. I was able to get it to work by simply adding a transform component:
    Code (CSharp):
    1.  cloneGrid = Instantiate(Grid_Floor, new Vector3(x, 0, y), Quaternion.identity);
    In future posts @Lurking-Ninja I'll be sure to post code properly. Thank you for the tip!
     
    Lurking-Ninja likes this.