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 /
avatar image
0
Question by george_vasilchenko · Oct 01, 2014 at 02:09 AM · instantiatedestroy

Destroy instance

Hello, I am making a game where a player has to avoid obstacles. As the borders of the level I use a "box" - like geometry that spawns as a list of gameobjects in a row and translates toward the player by y axis. Player's position is not changing by Y axis. So, as soon as the first box moves away from sight of player (camera), I destroy it, remove at index and it should spawn again as the last in the row. The problem is that the compiler is barking. I have: MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. The problem line is : MoveSpawnDestroyBlocks method. Before I've implemented StateMachine approach, it worked as a charm. Not anymore.

Please help me with it. What's wrong here..?

The spawning script it here:

using UnityEngine; using Code.States; using System.Collections.Generic;

public class Spawning : MonoBehaviour {

 public GameObject block_PF;
 public GameObject player;
 public GameObject obstacle_PF;
 public GameObject badPU_PF;
 public GameObject goodPU_PF;
 public GameObject collectablePU_PF;

 public List<GameObject> blocks;

 private bool readyToSpawnObstacle = false;
 private bool readyToSpawnBadPU    = false;
 private bool readyToSpawnGoodPU    = false;
 private bool readyToSpawnCollectablePU = false;


 public void BlocksStartSpawn()
 {
     for (int i = 0; i < 5; i++) 
     {
         float y_pos = i * -25;
         
         GameObject clone = Instantiate(block_PF) as GameObject;
         clone.transform.position = new Vector3(0, y_pos, 0);
         
         blocks.Add(clone);
     }
 }
 public void MoveSpawnDestroyBlocks(float blockMoveSpeed_)
 {
     for (int i = 0; i < blocks.Count; i++) 
     {
         blocks[i].transform.Translate(new Vector3(0, blockMoveSpeed_ * Time.deltaTime, 0));
         
         if(blocks[i].transform.position.y >= 25)
         {    
             GameObject clone = Instantiate(block_PF) as GameObject;
             clone.transform.position = new Vector3(0, blocks[i].transform.position.y - 100, 0);
             
             blocks.Add(clone);
             Destroy (blocks[0]);
             blocks.RemoveAt(0);
         }
     }
 }


 public void SpawnDestroyObstacles(float obstacleMoveSpeed_)
 {    
     if(readyToSpawnObstacle)
     {
         float z_rand = Random.Range(-2.0f, 2.0f);
         float rand_rot = Random.Range(0.0f, 180.0f);
         
         GameObject clone = Instantiate(obstacle_PF) as GameObject;
         
         clone.transform.position = new Vector3(0, -100, z_rand);
         clone.transform.rotation = Quaternion.Euler(0, rand_rot,0);
         clone.rigidbody.AddForce(0, obstacleMoveSpeed_, 0, ForceMode.Force);
         DestroyObject(clone, 6.0f);
     }
 }
 public void ObstSpawnCounter(float obstacleSpawnDelay_)    
 {
     float obstacleCounter = 0;

     obstacleCounter += Time.deltaTime;
     
     if(obstacleCounter >= obstacleSpawnDelay_)
     {
         readyToSpawnObstacle = true;
         obstacleCounter = 0;
     }
     else
     {
         readyToSpawnObstacle = false;
     }
     
 }


 public void SpawnDestroyBadPU(float badPUSpeed_)
 {
     if(readyToSpawnBadPU)
     {
         float rand_x_pos = Random.Range(-2.0f, 2.0f);
         float rand_z_pos = Random.Range(-2.0f, 2.0f);
         
         GameObject clone = Instantiate(badPU_PF, new Vector3(rand_x_pos, -125, rand_z_pos), Quaternion.identity) as GameObject;
         clone.rigidbody.AddForce(new Vector3(0, badPUSpeed_, 0), ForceMode.Force);
         clone.transform.Rotate(Time.deltaTime, Time.deltaTime, Time.deltaTime, Space.Self);
         readyToSpawnBadPU = false;
         
         DestroyObject(clone, 8.0f);
     }
 }


 public void SpawnDestroyGoodPU(float goodPUSpeed_)
 {
     if(readyToSpawnGoodPU)
     {
         float rand_x_pos = Random.Range(-2.0f, 2.0f);
         float rand_z_pos = Random.Range(-2.0f, 2.0f);
         
         GameObject clone = Instantiate(goodPU_PF, new Vector3(rand_x_pos, -125, rand_z_pos), Quaternion.identity) as GameObject;
         clone.rigidbody.AddForce(new Vector3(0, goodPUSpeed_, 0), ForceMode.Force);
         clone.transform.Rotate(Time.deltaTime, Time.deltaTime, Time.deltaTime, Space.Self);
         readyToSpawnGoodPU = false;
         
         DestroyObject(clone, 8.0f);
     }
 }


 public void SpawnDestroyCollectablePU(float collectablePUSpeed_)
 {
     if(readyToSpawnCollectablePU)
     {
         float rand_x_pos = Random.Range(-2.0f, 2.0f);
         float rand_z_pos = Random.Range(-2.0f, 2.0f);
         
         GameObject clone = Instantiate(collectablePU_PF, new Vector3(rand_x_pos, -125, rand_z_pos), Quaternion.identity) as GameObject;
         clone.rigidbody.AddForce(new Vector3(0, collectablePUSpeed_, 0), ForceMode.Force);
         clone.transform.Rotate(Time.deltaTime, Time.deltaTime, Time.deltaTime, Space.Self);
         readyToSpawnCollectablePU = false;
         
         DestroyObject(clone, 8.0f);
     }
 }


 public void PowerupsCounter(float badPUSpawnDelay_, float goodPUSpawnDelay_, float collectablePUSpawnDelay_)
 {
     float badPUCounter = 0.0f; 
     float goodPUCounter = 0.0f;
     float collectablePUCounter = 0.0f;

     badPUCounter += Time.deltaTime;
     goodPUCounter += Time.deltaTime;
     collectablePUCounter += Time.deltaTime;

     if(badPUCounter >= badPUSpawnDelay_) 
     {
         readyToSpawnBadPU = true;
         badPUCounter = 0;
     }
     if(goodPUCounter >= goodPUSpawnDelay_) 
     {
         readyToSpawnGoodPU = true;
         goodPUCounter = 0;
     }
     if(collectablePUCounter >= collectablePUSpawnDelay_) 
     {
         readyToSpawnCollectablePU = true;
         collectablePUCounter = 0;
     }
 }



}

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

"Destroy assets is not permitted to avoid data loss" 2 Answers

I need to Instantiate a prefab at the same location where a different object was destroyed., 1 Answer

Cannot destroy instantiated objects 2 Answers

Destroying a prefab doesn't remove it from the Hierarchy list 2 Answers

Instantiate problem 1 Answer


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