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 GameObject at runtime via Script.

Discussion in 'Scripting' started by sasuchi, Apr 12, 2020.

  1. sasuchi

    sasuchi

    Joined:
    Jun 18, 2019
    Posts:
    13
    Hello everyone,

    I'm not able to find an answer to this question.

    I have a script where I create a new GameObject and add relevant Components. When testing in the editor playmode everything works fine, when playing the build version its not working. After some debugging I could already narrow it down on the following part of the code:

    Code (CSharp):
    1.         public void PreviewSelection()
    2.     {
    3.         preview = new GameObject("Preview", typeof(Transform));
    4.         preview.tag += "Test";
    5.         preview.transform.SetParent(grid.transform);
    6.         preview.AddComponent<Tilemap>();
    7.         preview.AddComponent<TilemapRenderer>();
    8.         TileUtility.SetTiles(preview.GetComponent<Tilemap>(), currentSelection.tilemap, currentSelection.size, Vector3Int.zero);
    9.         preview.GetComponent<Tilemap>().color = new Color(1, 1, 1, 0.6f);
    10.         preview.GetComponent<TilemapRenderer>().sortingOrder = 3;
    11.         Debug.Log(preview);
    12.     }
    The declaration of the preview variable is inside the class

    Code (CSharp):
    1.     public GameObject preview;
    I already learned, that Assets/Resources are not available at runtime, if not explicitally made available through e.g. AssetBundle. What I dont get is why i cant create a new gameobject at runtime (i already imagine that theres something about the gameobject concept i dont fully grasp yet).

    Sorry for the bad english, im a bit out of practice.

    Thanks everyone!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
  3. sasuchi

    sasuchi

    Joined:
    Jun 18, 2019
    Posts:
    13
    Thank you for the fast reply.

    While your suggested Solution (probably) works, I still wonder if thats the only/best way given the circumstances. I should further explain, that the Script with the shown Method is added to a GameManager Object during Runtime (when a scene change happens). The idea is, that only during that scene the script is needed. With your solution I would have the component throughout every scene, which seems suboptimal (although this shouldnt cause any performance issues).
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Let's back up. It's completely fine to build a GameObject in the way you are doing in your code.

    Can you elaborate on what you mean by "its not working"? What specifically is not working? Is the object not getting created at all? Is there something wrong with the object's behavior?
     
  5. sasuchi

    sasuchi

    Joined:
    Jun 18, 2019
    Posts:
    13
    Okay, to answer you question, i was getting a null pointer - however after rechecking it again its actually not the "preview" variable, but a different one thats holding a scriptable object as data container that is pointing to null. I'll take a closer look at that track.

    Edit:

    Yes its the wrong assignment of the scriptable object. My current code has only the method AssetDatabase.LoadAssetAtPath to assign the SO's. And i already linked those in the GameManager, just need to fix the certain spot.
     
    PraetorBlue likes this.