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

Can 1 GameObject Variable Contain Many GameObjects?

Discussion in 'Scripting' started by gamecreatorc1, Mar 28, 2021.

  1. gamecreatorc1

    gamecreatorc1

    Joined:
    Dec 12, 2019
    Posts:
    54
    I have the following simple function that adds items dynamically to my level:

    Code (CSharp):
    1. void addgameobject(string name, Vector3 pos)
    2. {
    3.     GameObject item;
    4.  
    5.     item = Resources.Load(name) as GameObject;
    6.  
    7.     levelobject = (GameObject)GameObject.Instantiate(item, pos, Quaternion.Euler(new Vector3(0, 0, 0)));
    8. }
    This actually works perfectly fine right now and adds as many items as I want. I'm relatively new to C# but shouldn't levelobject be an array or a list or something? Or does Unity take care of this in the background for me?

    As an additional test, I've parented every object to a single other object (world) and when I Destroyed world, it deleted all the other objects I created.

    So, should I do this a better way (list of GameObjects) or is the above actually OK?

    (levelobject is defined as just GameObject levelobject elsewhere in the file)
     
  2. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    you put in the list when you plan to do something with it later and
    if you didn't have any errors from destroying the parent then it should be fine
     
    gamecreatorc1 likes this.
  3. gamecreatorc1

    gamecreatorc1

    Joined:
    Dec 12, 2019
    Posts:
    54
    Thank you. Seems to not have any issues after several hours of working with the project like that.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Side note: you really should use this form:

    Code (csharp):
    1. item = Resources.Load<GameObject>(name);
    Reason: if you have some other kind of asset (Texture or Material for instance) named exactly the same, it is a 50/50 chance which one Unity will load, since Resources.Load() does NOT accept the file extension. If it tries to load
    foobar.png
    instead of
    foobar.prefab
    , it will fail 100% of the time. Specifying the <T> is the only way to make sure it goes after the prefab.

    As a case of style, you should CamelCase your functions, and you should stay away from using identifiers that are already in use inside the MonoBehavior, in this case
    name
    .

    Personally I would rename the above to:

    Code (csharp):
    1. void AddGameObject(string resourceName, Vector3 position)
    2. {
    3. }
    Reason: when you ask for help, your code is immediately more readable and less likely to have weird problems from hiding existing identifiers. For instance, if you tried to access the
    name
    field of the UnityEngine.Object in this method, you would not get it unless you explicitly wrote
    this.name
    , and that could be very confusing.

    But it is of course your codebase.
     
    Putcho and gamecreatorc1 like this.
  5. gamecreatorc1

    gamecreatorc1

    Joined:
    Dec 12, 2019
    Posts:
    54
    Hey, thank you for the tips! I really appreciate it and I'm going to implement them. It's especially easy now since I'm still very early in the project.