Instantiate keep on cloning my object - How can I stop it!?
I'm trying to make a destructible object by using 2 codes that I learned from a tutorial. http://gamedevelopment.tutsplus.com/tutorials/how-to-make-an-object-shatter-into-smaller-fragments-in-unity--gamedev-11795
At first, it was somewhat working. But after I added a few more codes of mine in it, it got messed up. It keeps on cloning and made my whole game lagging.
using UnityEngine; using System.Collections;
public class Destroy : MonoBehaviour
{
public GameObject remains;
private PlayerInteract securityCheck;
public bool destroyed = false;
// Use this for initialization
void Start()
{
destroyed = false;
securityCheck = GameObject.Find("Player").GetComponent<PlayerInteract>();
}
// Update is called once per frame
void Update()
{
if (securityCheck.doorOpen == true)
{
StartCoroutine(BOMB());
destroyed = true;
}
}
IEnumerator BOMB()
{
yield return new WaitForSeconds(1);
Instantiate(remains, transform.position, transform.rotation);
Destroy(gameObject);
}
}
This is the 2nd code. It did destroyed my original object but didn't do anything with the clones.
public class SelfDestruct : MonoBehaviour {
private Destroy destroy;
public ParticleSystem explode;
// Use this for initialization
void Start () {
destroy = GameObject.Find("Cube fragment").GetComponent<Destroy>();
}
// Update is called once per frame
void LateUpdate () {
if (destroy.destroyed == true)
{
ParticleBoom();
StartCoroutine(Destroy());
Destroy(explode);
}
}
IEnumerator Destroy()
{
yield return new WaitForSeconds(2);
Destroy(gameObject);
Debug.Log("Bang Bang Bang");
}
void ParticleBoom()
{
explode = (ParticleSystem)Instantiate(explode, transform.position, Quaternion.identity);
}
}
And my explode particle system isn't working properly either, it keeps on cloning and exploding, so I had to destroy its "explode".
Your answer
Follow this Question
Related Questions
How do you make a clone have the same tag as its original? 0 Answers
Bullet clones will not be destroyed. 2 Answers
How to create an array of clones for a seperate Save/Load script 0 Answers
Random.range game object destroying itself before reaching destroy position.Please help. 1 Answer
Destroy button only destroys the last clone of object 1 Answer