Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Instantiating a new GameObject in C#

Discussion in 'Scripting' started by runbellerun, Sep 15, 2008.

  1. runbellerun

    runbellerun

    Joined:
    Jul 25, 2007
    Posts:
    57
    Hi all,
    This should be a simple problem, but I haven't been able to solve it yet!
    I am trying to instantiate a bunch of 'null' gameobjects (ie with position 0,0,0 and rotation = identity) as helpers in my scene, without the need to drag in a prefab first. (Yes, I know I can just drag in a prefab.. but let's just assume this is for argument's sake! :) )

    Is there a way to do this:
    GameObject thing = new GameObject("Thingy", GameObject.transform.position(0,0,0), GameObject.transform.rotation(Quaternion.identity));

    ?

    I'm using this definition from the scripting reference:
    static function GameObject (name : string, params components : Type[]) : GameObject
    Description
    Creates a game object and attaches the specified components.

    But maybe I don't know how to pass in the parameters properly?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,775
    Code (csharp):
    1. GameObject thing = new GameObject("Thingy");
    It has a position of Vector3.zero and a rotation of Quaternion.identity by default. Position and rotation aren't components anyway; they're just elements of the Transform component.

    --Eric
     
  3. runbellerun

    runbellerun

    Joined:
    Jul 25, 2007
    Posts:
    57
    Ah yeah that's true.

    I also notice you can't instantiate a transform directly, so you can't just say:

    Transform blah = new Transform();

    Cos what I really want is a bunch of helper TRANSFORMS, but I'm having to instantiate gameobjects and then use their transforms. Is that correct?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,775
    Yeah, you can't have loose components without a GameObject to hang them on. Also you can't have a GameObject with zero components, so making an empty GameObject like that automatically adds a Transform component.

    Hey, as long as you've brought this up, not that it really applies after all, but anyway:

    As far as the "GameObject (name : string, params components : Type[])" method goes, to use that you'd apparently supply an array of System.Type[]. Something like (Javascript):

    Code (csharp):
    1. var someComponents = new System.Type[2];
    2. // Define someComponents[0] and someComponents[1] here
    3. var thing = new GameObject("Thingy", someComponents)
    Only thing is, I tried using that once and couldn't figure out how you get UnityEngine.x (where x = Rigidbody, etc.) to System.Type. So if anyone knows, tell me please. :)

    --Eric
     
  5. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    The function signature has the 'params' keyword for the array of types, which means you can add the remaining parameters to the end of the function call instead of putting them in an array:
    Code (csharp):
    1. var yourGameObject = new GameObject("Name", Rigidbody, SphereCollider, YourScript);
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,775
    Aha...actually I tried that first and for some reason it didn't work; obviously I did something stupid and then made a wrong assumption. Thanks!

    --Eric
     
  7. tobmuell

    tobmuell

    Joined:
    Nov 12, 2012
    Posts:
    4
    This does not work for me. The compiler says it´s a type "type" but used as a variable. What am I doing wrong?
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,443
    Rafarel and FUBAR2077 like this.
  9. Rafarel

    Rafarel

    Joined:
    Jul 21, 2017
    Posts:
    3
    Thank you for this answer KelsoMRK.
    typeof is needed!