Why does my spawn manager not spawn my prefabs after my original object is destroyed [tutorial question]
Hi everyone, I'm currently learning Unity and following the unity tutorial for lesson 2.4.
One issue I've run into is that when my original object go out of bounds and is destroyed, they no longer spawn the prefabs and gives me a "MissingReferenceException". Also in the inspector tab for my RandomSpawn script, element 0, 1, and 2 shows "Missing (Game Object). All of my prefabs also contain the same scripts as the original object as well. I'm following the tutorial and doing it exactly how they do it and it seems to work for them but it does not run properly for me and I'm not too sure why.
Any help would be appreciated.
Here is my code for the RandomSpawn script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomSpawn : MonoBehaviour
{
public GameObject[] animalArray; //create a game object array to hold 3 animal prefabs.
private float lowerSpawnBound_X = -12.61f;
private float upperSpawnBound_X = 15.33f;
private float spawn_z_Position = 18.38f;
private float startTime = 2f; //initial time before first spawn
private float repeatTime = 1.5f; //spawn every 2s after
// Start is called before the first frame update
void Start()
{
//spawn the animals on a timer.
InvokeRepeating("spawnRandomAnimal", startTime, repeatTime); //Calls the method for the first time after 'x' seconds, and then repeats the call every 'y' seconds after.
}
// Update is called once per frame
void Update()
{
}
private void spawnRandomAnimal() //giving error that object is already destroyed
{
int animalIndex = Random.Range(0, animalArray.Length);
Vector3 spawnPos = new Vector3(Random.Range(lowerSpawnBound_X, upperSpawnBound_X), 0, spawn_z_Position);
Instantiate(animalArray[animalIndex], spawnPos, animalArray[animalIndex].transform.rotation);
}
}
Answer by brendangrburke · Apr 16, 2021 at 04:27 AM
You did not make a mistake with writing your code. I came upon your question while I had run into the same problem last night going through the create with code course. I had added the animals or (GameObjects) from my Hierarchy instead Project into my Array list. The difference is as far as I can figure is that Hierarchy is exclusive to the Level or Scene and things in Project can be accessed in or by any Scene or Level. So the animals I added weren't the true Prefabs,I think I know where you went wrong you dragged the animals/game objects from your Hierarchy instead of Project into the Array or animalIndex If you were using the "create with code" course. From what I found when I was trying to find an answer this myself is that Hierarchy is what is happening or accessible in the Level or Scene & Project is what's accessible or can interact across the game Or game Project ; )
But I only started learning this stuff a little more then a month ago so I'm not 100% sure you can trust my answer
Your answer
Follow this Question
Related Questions
Spawning objects with force so it looks like someone is randomly throwing stuff 1 Answer
How do I make the enemies continue spawning? 1 Answer
How to stop Enemys spawning every frame from every spawn point? 1 Answer
IEnumerator project problem 0 Answers
question about object pooling an array and spawning 0 Answers