Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by the3nding · Dec 24, 2020 at 11:35 AM · spawning problems

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);
         }
     }
      
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

155 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges