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

To or not to instantiate Game Object in a List???

Discussion in 'Scripting' started by shenglee83, Sep 6, 2016.

  1. shenglee83

    shenglee83

    Joined:
    Nov 19, 2012
    Posts:
    2
    Hello and Good day to everyone,

    As per my title, I would like to know is it a good idea/bad idea to instantiate Game Object into a List, given with both example below:

    Code (CSharp):
    1.  
    2. List<GameObject> gObjList;
    3.  
    4. void Start(){
    5.                 gObjList = new List<GameObject> ();
    6.      
    7. //To add game object into the list by the following approach
    8.         gObjList.Add(gObj);
    9.  
    10.                 // or by this approach
    11.                 GameObject newObj =  (GameObject)Instantiate(GameObject);
    12.        gObjList.Add (newObj);
    13. }
    14.  
    15.  
    which approach is more preferable and why use/not to use if the other one is not preferable????
     
    Last edited: Sep 6, 2016
  2. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Hello !

    It seems like the first part of your example is missing a bit, i'm guessing you meant to add:

    Code (csharp):
    1. GameObject gObj = new GameObject();
    ?

    Anyways you're not really instantiating your gameObjects into your lists.

    Calling "new GameObject()" can be used to create objects from scratch and calling "Instantiate(object)" can be used to clone an existing hierarchy, either a prefab asset or an actual gameObject from your scene, and it works either in edit mode or at runtime.

    Both are perfectly valid approaches based on context, if you have some procedurally generated content you might want to customize it at runtime by creating it from scratch whereas if you're spawning waves of predefined creatures you might want to look into using prefabs. You can also mix both and customize prefabs with extra flavour and toppings.

    What you do with the returned value is merely storing a reference to those GO you created, which is totally fine either way.

    Something worth noting is that the return value of Instantiate() is of the same type as its first parameter*, so if you feed it a component it will return the component reference even though it created the whole hierarchy of that gameObject the component is on.


    Have a good day as well !


    *: referenced into a UnityEngine.Object that you need to cast to the proper type if not using the generic version
     
    Last edited: Sep 6, 2016
    Purplepigfarm and shenglee83 like this.
  3. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    This is incorrect. The return value of Instantiate is always UnityEngine.Object and will need to be casted to the correct type. Instantiate<T>(obj) will return an object of type T:
    Code (CSharp):
    1. var obj = Instantiate(prefab);
    2. // obj is of type UnityEngine.Object
    3.  
    4. var obj = Instantiate<GameObject>(prefab);
    5. // obj is of type GameObject
     
    shenglee83 likes this.
  4. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Apologies I didn't phrase it correctly, I indeed meant to point out that the value of the object should be casted to the type of the first parameter, and not to always GameObject or Transform as people often make the mistake with the version of Instantiate() that takes position & rotation parameters.
     
    Last edited: Sep 6, 2016
  5. shenglee83

    shenglee83

    Joined:
    Nov 19, 2012
    Posts:
    2
    Hi and thank you met44 and Timelog... the explanation makes everything much clearer...
     
    _met44 likes this.