Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

CreatePrimitive vs. new GameObject

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

  1. drhmiri


    Jun 17, 2014
    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


    Apr 17, 2013

    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:

    See the link below for instantiate method:
    drhmiri likes this.
  3. kdgalla


    Mar 15, 2013
    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


    Jul 23, 2018
    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


    Feb 15, 2012
    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.