Quantcast
Channel: Questions in topic: "clone"
Viewing all articles
Browse latest Browse all 508

Missing reference exception in Unity after instantiating multiple GameObject prefabs and deleting original

$
0
0
I am currently making a Breakout style game and have 2 "Power-ups" that are giving me some trouble. I have a double-ball and triple-ball power-ups that instantiate 1 or 2 extra balls from an original ball prefab. Everything works fine as long as the original ball is still in the scene but after the original ball from the scene is deleted (The ball is destroyed after it hits the bottom of the screen but the game continues if the cloned balls are still in play and the "Ball Count" is not 0) I get the following error in the lines containing the Instantiate function in the double and triple power-up cases: MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. The portion of my Paddle Code handles the powers on collision between the paddle and the powerup is as follows: void OnTriggerEnter2D(Collider2D collision) { GameObject[] ArrayOfBalls = GameObject.FindGameObjectsWithTag("Ball"); switch (collision.tag) { case "Life Powerup" : Debug.Log("hit " + collision.name); gm.UpdateLives(1); Destroy(collision.gameObject); break; case "Speed PowerUp": for (int i = 0; i(); if (ballscript.rb.velocity.magnitude > 7.5f) { CancelInvoke("NormalSpeed"); Invoke("NormalSpeed", 5f); } else if (ballscript.rb.velocity.magnitude <= 7.5f) { ballscript.rb.velocity = new Vector2(ballscript.rb.velocity.x * 1.5f, ballscript.rb.velocity.y * 1.5f); Invoke("NormalSpeed", 5f); } } //If another powerup is collected call to function to slow down is cancelled and started with new delay Debug.Log("hit " + collision.name); Destroy(collision.gameObject); break; case "Shrink PowerDown": gameObject.transform.localScale = new Vector3( 0.25f, 0.4f, 1f); Invoke("NormalSize", 15); Debug.Log("hit " + collision.name); Destroy(collision.gameObject); break; case "Expand PowerUp": gameObject.transform.localScale = new Vector3( 0.55f, 0.4f, 1f); Invoke("NormalSize", 5f); Debug.Log("hit " + collision.name); Destroy(collision.gameObject); break; case "Double PowerUp": for (int i = 0; i < ArrayOfBalls.Length; i++) { ball ballscript = ArrayOfBalls[i].GetComponent(); Transform Doubleclone = Instantiate(PrefabBall, new Vector3(ballscript.rb.position.x + 0.1f, ballscript.rb.position.y + 0.1f, 0f), Quaternion.identity); Doubleclone.GetComponent().velocity = new Vector2(ballscript.rb.velocity.x, ballscript.rb.velocity.y); gm.UpdateBallCount(1); } Debug.Log("hit " + collision.name); Destroy(collision.gameObject); break; case "Triple PowerUp": for (int i = 0; i < ArrayOfBalls.Length; i++) { ball ballscript = ArrayOfBalls[i].GetComponent(); Transform Tripleclone = Instantiate(PrefabBall, new Vector3(ballscript.rb.position.x + 0.1f, ballscript.rb.position.y + 0.1f, 0f), Quaternion.identity); Transform Tripleclone2 = Instantiate(PrefabBall, new Vector3(ballscript.rb.position.x - 0.1f, ballscript.rb.position.y - 0.1f, 0f), Quaternion.identity); Tripleclone.GetComponent().velocity = new Vector2(ballscript.rb.velocity.x, ballscript.rb.velocity.y); Tripleclone2.GetComponent().velocity = new Vector2(ballscript.rb.velocity.x, ballscript.rb.velocity.y); gm.UpdateBallCount(2); } Debug.Log("hit " + collision.name); Destroy(collision.gameObject); break; } I don't know what a good fix for this would be and appreciate any ideas!

Viewing all articles
Browse latest Browse all 508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>