Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Create empty GameObject dynamically

Discussion in 'Scripting' started by ldrrn, Mar 30, 2010.

  1. ldrrn

    ldrrn

    Joined:
    Jan 26, 2010
    Posts:
    7
    Hi, is there anyway to create empty gameobject by using script only? i need to create a path based on xml data in runtime. thanks in advance
     
    RPLaine and stepan-stulov like this.
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. var go = new GameObject();
    --Eric
     
    Nemario, BitGamey, mcroswell and 11 others like this.
  3. ldrrn

    ldrrn

    Joined:
    Jan 26, 2010
    Posts:
    7
    excellent! i thought i need an api to do that. thanks
     
  4. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    725
    Been learning Unity recently and would like to add something else to this, since I found it searching on google and it was helpful, but:

    Code (CSharp):
    1. var go = new GameObject().AddComponent<AudioSource> ();
    2. go.name = "Music Player";
    You can add components like that. I'm using something like this for playing music dynamically using only code. You can add any component you'd like, though, like a mesh and then a rigid body to create an object.

    I don't know what others thoughts on this are, but I felt like it's better practice to have a player start position on the game map, but then maybe have the entirety of the player object created dynamically by just code. It feels like there's a lot more control that way to me. In fact, I just feel like it's better practice if you're going to have tons of scenes as maps to have global objects created through script, like a global SFX Player, Music Player, Player object, etc... That way you don't have to re-create any objects in mapping, so mapping is just mapping.
     
    RunninglVlan, Richard5635 and Zascal like this.
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I'd have to quibble with that code and suggest that the variable name be "audioSource", since it's referring to the AudioSource component and not the GameObject. As far as best practices, I'd say that it depends, but it's more likely to be a better practice to use a prefab. That way level designers can tweak parameters as necessary for each level, and they won't have to recreate any objects. Just drop the prefab in.

    --Eric
     
    mcroswell, Westland and Kurt-Dekker like this.
  6. derHugo

    derHugo

    Joined:
    Nov 1, 2017
    Posts:
    12
    This can (now?) be also achieved using only

    Code (CSharp):
    1. new GameObject("Music Player", AudioSource);
    which I find more readable
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,371
    Not quite, it's
    new GameObject("Music Player", typeof(AudioSource));


    I've never found the Type[] you're using there useful. If I'm adding components, I generally need to do something with them, so using it would require me to GetComponent right after, so I might as well use AddComponent. The method is also a params method, so (afaik) it allocates an array for the elements you supply, even if you're only adding one component.
     
    ebarlowsmith likes this.