Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Creating unique idenifiable clones of pre-made objects

Discussion in 'Scripting' started by SwissAndOr, May 3, 2014.

  1. SwissAndOr

    SwissAndOr

    Joined:
    May 3, 2014
    Posts:
    8
    Let's say I want to spawn an object multiple times but some way, make them identifiable so a script can move them or change them individually. Preferably one script that can be attached to a camera to change and move all of the other objects. The script would spawn and would be able to identify all the spawned objects and change them individually. Thanks beforehand!
     
    Last edited: May 3, 2014
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Create an array and put the clones returned by Instantiate into the array.

    Code (csharp):
    1. gameObjects = new GameObject[100];
    2. for (var i = 0; i < gameObjects.Length; i++) {
    3.     gameObjects[i] = Instantiate(prefab);
    4. }
    --Eric
     
  3. SwissAndOr

    SwissAndOr

    Joined:
    May 3, 2014
    Posts:
    8
    Thanks a lot! The objects were going to spawn in a 2D grid anyways so I can refer to them by their position which is quite nice. Haven't tried it yet but I see how it's gonna work.