Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Prefab Only Instantiates if Game Object in Hierarchy

Discussion in 'Prefabs' started by kylesmithsystems, May 8, 2024.

  1. kylesmithsystems

    kylesmithsystems

    Joined:
    May 7, 2024
    Posts:
    6
    Hello Unity Prefabs,

    I am working on a project to learn about Unity. This is my first Unity game. I am following this "Create with Code" tutorial and am working on building my personal project: https://learn.unity.com/course/create-with-code?uv=2022.3

    My problem is I need the same game object in the Hierarchy and in the Assets/Prefab folder for a call to the "Instantiation" method to work close to as expected.

    When I was following along with the tutorials I did not have this issue. Now I have to hide the game objects away from the camera so they do not show up until the call to Instantiation puts them in the scene.

    What am I missing please?

    Here is some code where the problem appears:

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3.  
    4. public class SpawnWarp : MonoBehaviour
    5. {
    6.     public GameObject warpPrefab;
    7.     private float spawnDelay = 5f;
    8.     private float spawnAreaWidth = 24f;
    9.     //private float spawnAreaLength = 8f;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         Debug.Log("[+] Start called in SpawnWarp!");
    15.         InvokeRepeating("Spawn_A_Warp", spawnDelay, 30f);
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         // Debug.Log("MoveGunPowerUp here!");
    22.     }
    23.  
    24.     void Spawn_A_Warp()
    25.     {
    26.         Debug.Log("[+] Spawning a warp!");
    27.         // Generate random position within spawn area
    28.         float randomX = UnityEngine.Random.Range(-spawnAreaWidth / 2f, spawnAreaWidth / 2f);
    29.  
    30.         float randomZ = UnityEngine.Random.Range(-6f, 1.5f);
    31.  
    32.         Vector3 randomPosition = new Vector3(randomX, 1.1f, randomZ);
    33.         Debug.Log("About to Instantiate Warp!");
    34.  
    35.         var new_warp = Instantiate(warpPrefab, randomPosition, Quaternion.identity);
    36.         Debug.Log("Warp Prefab Instantiated!");
    37.  
    38.         Destroy(new_warp, 15f); // Destroy the warp after 15 seconds
    39.  
    40.     }
    41. }
    42.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,161
    You can instantiate literally any Unity object. It doesn't need to be in a scene, or in your assets, necessarily.

    There should be no reason for this. You just need to reference the prefab (in your assets), and then you instantiate said prefab. Are you just mistakingly referencing the object in the scene instead?
     
  3. kylesmithsystems

    kylesmithsystems

    Joined:
    May 7, 2024
    Posts:
    6
    That must be the issue. I am dragging the prefab into the inspector for the script component (see attached image). I have the game object as a public variable in the C# script, so this is possible. When I double-click on the game object in the inspector the prefab highlights, which seems to say that is the object to be instantiated.

    There must be something else going on because I have to have the prefab in the Hierarchy, hiding off camera, and in the Prefabs folder for the code to work as planned.

    If I re-name the prefab so the prefab has a unique name and delete the game object of the same thing in the Hierarchy nothing spawns. If I move the prefab into the hierarchy and have the prefab in the prefabs folder the object instantiates as expected. I have no idea what is going on. 0_o Screenshot 2024-05-07 at 11.05.07 PM.png
     
  4. kylesmithsystems

    kylesmithsystems

    Joined:
    May 7, 2024
    Posts:
    6
    I figured out the problem thanks to help from Bunny83 in another part of the forum! I had the C# script to instantiate the prefabs attached to the prefabs and objects themselves. The solution is to create a "spawn manager" and instantiate the prefabs from this spawn manager! :)