Search Unity

CreatePrimitive vs. new GameObject

Discussion in 'Scripting' started by drhmiri, Aug 6, 2019.

  1. drhmiri

    drhmiri

    Joined:
    Jun 17, 2014
    Posts:
    76
    I was trying to create a new game object called Orb and add the necessary components to it, but I realized only the transform was being created, so I couldn't see any object:
    Code (CSharp):
    1. Orb = new GameObject("Orb");
    2. Orb.AddComponent<MeshRenderer>();
    3. Orb.AddComponent<BoxCollider>();
    4. Orb.AddComponent<MeshFilter>();
    5. Orb.AddComponent<Rigidbody>();
    Then, I tried the following and unsurprisingly could see a sphere:
    Code (CSharp):
    1. GameObject.CreatePrimitive(PrimitiveType.Sphere);
    The reason I could not see a game object is because new GameObject does not have a particular game object specified to create, right?

    Also, is there anyway I could create a sphere and call it Orb in one line of code?
     
  2. emre2345DH

    emre2345DH

    Joined:
    Apr 17, 2013
    Posts:
    26
    Hello,

    Unity is a component based game engine which means you add functionality to game objects by adding new components to it. GameObject is an entity in a scene so it does not have any graphical representation. Transform is the only component that all gameobjects has to have. The reason is gameobject is an object standing in the 3d world, thus it needs position, scale and rotation which is provided by transform component.

    CreatePrimitive method creates a gameobject which has a mesh filter and mesh renderer.

    The way you might proceed with preparing a gameobject with the desired components on it and saving it as a prefab in the project. Then you might instantiate this gameobject with one line of code.

    See the links below for what a prefab is:
    https://docs.unity3d.com/Manual/Prefabs.html


    See the link below for instantiate method:
    https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
     
    drhmiri likes this.
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,533
    There aren't any "particular" GameObjects in Unity, there's only one type of GameObject. Everything is determined by the components that are attached to the game object. Unity's Sphere primitive is just an ordinary GameObject whose mesh filter component happens to have a sphere mesh set as it's mesh.

    On the other hand, when you call "AddComponent<MeshFilter>();" what mesh is your object's mesh filter set to? I'm guessing it's no mesh at all. Hypothetically, if you want to add a mesh filter in your code, you'd have to also set the MeshFilter's mesh to some value.

    If you want to create a complicated object with lots of components at runtime, the easiest way is to make a prefab of the object and use Instantiate.
     
    drhmiri likes this.
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,131
    You've added all the components required to draw a mesh in the scene, but you haven't configured it with mesh and material, so it displays nothing. Set mesh to mesh filter and material to mesh renderer, then it will be visible (if shader is supported on your hardware of course).
     
    drhmiri likes this.
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    726
    Yes, Instantiate on prefabs is the normal, common way to create objects.

    Unity has "new GameObject" and examples about making objects entirely with AddComponents since that's how normal programs work -- you make your items completely through code. It's a huge pain. To a pure programmer, Instantiate is just weird -- you're making a copy of something which just magically exists when the program starts.

    It turns out the "new GameObject("orb"); is overloaded. It makes a gameObject, also creates and adds a transform with that name, and inserts the whole thing into the master list of game objects.
     
    drhmiri likes this.