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

Can't instantiate and get a referene to the GameObject

Discussion in 'Scripting' started by Deeblock, May 4, 2018.

  1. Deeblock

    Deeblock

    Joined:
    Feb 14, 2018
    Posts:
    49
    Code (CSharp):
    1. // Instantiate required objects
    2.         // Trees
    3.         var treeParent = new GameObject();
    4.         treeParent.name = "Trees";
    5.         foreach(TreeSaveLoad.TreeData tree in gameData.trees)
    6.         {
    7.             var treeType = redwoodTree;
    8.             switch (tree.type)
    9.             {
    10.                 case (TreeSaveLoad.TreeType.Redwood):
    11.                     treeType = redwoodTree;
    12.                     continue;
    13.                 case (TreeSaveLoad.TreeType.Birch):
    14.                     treeType = birchTree;
    15.                     continue;
    16.                 case (TreeSaveLoad.TreeType.Beech):
    17.                     treeType = beechTree;
    18.                     continue;
    19.                 case (TreeSaveLoad.TreeType.Pine):
    20.                     treeType = pineTree;
    21.                     continue;
    22.                 case (TreeSaveLoad.TreeType.Oak):
    23.                     treeType = oakTree;
    24.                     continue;
    25.                 case (TreeSaveLoad.TreeType.Maple):
    26.                     treeType = mapleTree;
    27.                     continue;
    28.             }
    29.             GameObject treeChild = Instantiate(treeType, tree.position, Quaternion.identity) as GameObject;
    30.         }
    I have 6 prefabs which are different types of trees. When I instantiate them based on their stored position and type, I want to be able to reference the spawned object in order to place them under an empty parent object "Trees" so that they don't clutter up the editor view. However, Unity does not let me Instantiate it as a GameObject. How can I get a reference to the instantiated object? Thanks.

    The error thrown is "'Cannot convert UnityEngine.Transform' to 'UnityEngine.GameObject' via a built-in conversion."
     
  2. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    > The error thrown is "'Cannot convert UnityEngine.Transform' to 'UnityEngine.GameObject' via a built-in conversion."
    This is very important! So you instantiate something and get a Transform back, that is not an GameObject. You can cast the result of Instantiate to a Transform though, and then get the gameObject property.
     
  3. Deeblock

    Deeblock

    Joined:
    Feb 14, 2018
    Posts:
    49
    Ok, putting it as a Transform instead of a GameObject works. However, my code does not run because apparently "continue" breaks the foreach loop. Is there a way to exit the switch loop alone?
     
  4. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
  5. Deeblock

    Deeblock

    Joined:
    Feb 14, 2018
    Posts:
    49
    Thanks for the help!