Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Creating a GameObject from code not allowed

Discussion in 'Scripting' started by yxyx136, Mar 5, 2016.

  1. yxyx136

    yxyx136

    Joined:
    Aug 29, 2014
    Posts:
    3
    I'm trying to create another VoxelEngine using chunks which have to be created on the fly.
    By creating them using the "new" keyword, (as I did it in previous Unity versions) I'm receiving an odd warning:
    You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent().
    I've searched for some solutions and found alot of custom classes to bypass the problem, but none of them seem to fit in my case.

    In general I just want to create GameObjects (called chunks), and save them inside a Dictionary.

    That's how it looks like:
    In class ChunkManager:
    Code (CSharp):
    1. Dictionary<ChunkVector, Chunk> chunks = new Dictionary<ChunkVector, Chunk>();
    2.  
    3.     public void CreateChunkAt(ChunkVector index)
    4.     {
    5.         GameObject chunk = new GameObject("Chunk " + index.ToString());
    6.         chunk.AddComponent<Chunk>();
    7.         chunk.transform.parent = gameObject.transform;
    8.         chunk.transform.position = index.GetPosition();
    9.         chunks.Add(index, chunk.GetComponent<Chunk>());
    10.     }
    which is initialized in class Terrain (inherited from MonoBehaviour):
    Code (CSharp):
    1. public class Terrain : MonoBehaviour {
    2.  
    3.     ChunkManager chunkManager;
    4.     // Use this for initialization
    5.     void Start () {
    6.         chunkManager = gameObject.AddComponent<ChunkManager>();
    7.         chunkManager.CreateChunkAt(new ChunkVector(0, 0, 0));
     
  2. DevEmpTy

    DevEmpTy

    Joined:
    May 1, 2015
    Posts:
    1
    Hey mate,
    I am a beginner myself so sorry if I cant really fix it but I will try to.

    //Edit: I checked it , this was wrong sorry
     
  3. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Use addcomponent. Mono behaviors (most scripts) are components. Can't have them alone, they attach to gameobjects.
     
    Kiwasi likes this.
  4. yxyx136

    yxyx136

    Joined:
    Aug 29, 2014
    Posts:
    3
    Thanks for your answer! Everyone recommends to use the "AddComponent" function, but I really don't know how to use it to create an actual GameObject? Is it even possible? I was only able to add scripts by using "AddComponent", but no GameObjects
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Use the new keyword for GameObjects. Use AddComponent for MonoBehaviours.
     
  6. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    You dont use AddComponent for this. you use Prefabs the "core" of Unity..
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Prefabs are nice, but I prefer spinning stuff up directly in code, since it's much easier to version control your changes than with prefabs.

    If you want to use the idiom of "make me a new MonoBehavior of this type" in code, I like this factory pattern, as it encapsulates all the incoming arguments and keeps everything private, returning you a reference to the new Monobehavior. You can use .Create() or .Attach(), depending on if you already have a game object you want to add the new monobehavior to.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyScript : MonoBehaviour
    5. {
    6.     private int data1;
    7.     private string data2;
    8.  
    9.     // when you want to create a fresh game object with a MyScript on it
    10.     public static MyScript Create( int data1, string data2)
    11.     {
    12.         MyScript myScript = new GameObject ("MyScript.Create();").AddComponent<MyScript> ();
    13.         myScript.data1 = data1;
    14.         myScript.data2 = data2;
    15.         return myScript;
    16.     }
    17.     // when you already have a game object and want to add a MyScript
    18.     public static MyScript Attach( GameObject go, int data1, string data2)
    19.     {
    20.         MyScript myScript = go.AddComponent<MyScript> ();
    21.         myScript.data1 = data1;
    22.         myScript.data2 = data2;
    23.         return myScript;
    24.     }
    25.  
    26.     void Update ()
    27.     {
    28.         // etc...
    29.     }
    30. }
     
    Kiwasi likes this.
  8. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    I dont understand why complicating things is better. You take away the most powerful tool in Unity.

    This code will generated overhead.