Search Unity

Resolved SpawnManager Not Spawning Items

Discussion in 'Getting Started' started by Ghost-Bunny, Feb 2, 2024.

  1. Ghost-Bunny

    Ghost-Bunny

    Joined:
    Jan 9, 2022
    Posts:
    5
    I'm doing Challenge 3, the Bombs and Ballon, but my SpawnManager isn't working! I fixed the error and checked everything, but nothing is happening. What am I doing wrong?!


    Code (CSharp):
    1. public GameObject[] objectPrefabs;
    2.     private float spawnDelay = 2;
    3.     private float spawnInterval = 1.5f;
    4.  
    5.     private PlayerControllerX playerControllerScript;
    6.  
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         InvokeRepeating("SpawnObjects", spawnDelay, spawnInterval);
    11.         playerControllerScript = GameObject.Find("Player").GetComponent<PlayerControllerX>();
    12.     }
    13.  
    14.     // Spawn obstacles
    15.     void SpawnObjects ()
    16.     {
    17.         // Set random spawn location and random object index
    18.         Vector3 spawnLocation = new Vector3(30, Random.Range(5, 15), 0);
    19.         int index = Random.Range(0, objectPrefabs.Length);
    20.  
    21.         // If game is still active, spawn new object
    22.         if (!playerControllerScript.gameOver == false)
    23.         {
    24.             Instantiate(objectPrefabs[index], spawnLocation, objectPrefabs[index].transform.rotation);
    25.         }
     
  2. APSchmidt

    APSchmidt

    Joined:
    Oct 31, 2023
    Posts:
    262
    Are you sure playerControllerScript is found? Try making it public and assigning it in the Inspector to make sure that it is.
     
  3. Ghost-Bunny

    Ghost-Bunny

    Joined:
    Jan 9, 2022
    Posts:
    5
    Okay, I made it public and it had nothing assigned to it. I assigned the Player, cause it was the only thing I could assign, but nothing happened.
     
  4. Ghost-Bunny

    Ghost-Bunny

    Joined:
    Jan 9, 2022
    Posts:
    5
    Okay, I restarted the Challenge and I redid everything, but I have no idea what I did wrong in the first place!
     
  5. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    Have you compered with answers? LINK