- Home /
Destroy 1 prefab object and changing the color/image
I want to destroy a created prefab (not all the prefab) and change the color or creating a new prefab on it. I figure out how to create a new image by using Instantiate on the destroyed position and/or changing the color by using renderer material color. But I want to know how to destroy 1 prefab/clone object that use Instantiate first before doing the rest. Anyone can give me hint or idea on how to do it? Tried using Destroy(this.gameObject), Destroy(gameObject), Destroy(GameObject.FindWithTag), etc but they destroy all of my life.
using UnityEngine;
using System.Collections;
public class Life : MonoBehaviour {
private GameObject gc;
private GameControl gcs;
public GameObject lifePrefab;
// Use this for initialization
void Start () {
gc = GameObject.Find ("GameControl");
gcs = gc.GetComponent<GameControl>();
}
// Update is called once per frame
void Update () {
// Get the current life from GameControl
if(gcs.getLife() == 2){
// Some code here to destroy 1 prefab life
// Create new different prefab at the same position
Vector3 newLifePos = new Vector3(-1.4f,0.6f,0);
Instantiate(lifePrefab, newLifePos, Quaternion.identity);
}
}
}
use destroy(this,1) ! this script should be on the prefab ! then it destroyed after its create after 1 second ! not matter it have the same name by the other cloned object
Thanks for the comment, but I just wanted to destroy one of the prefab(life) based on how many life I get deducted.
Answer by Ericool · Dec 14, 2014 at 01:49 AM
If I may if your prefab is just a texture you should keep the reference and just change the maintexture. But in your case I suggest you to do Destroy(gameObject) then in your gameObject script put the method OnDestroy(){Instantiate(another)}; Or if you have destructor ~MyClass that's fine too , but it cannot be inherited from MonoBehaviour although .
Thanks for the reply, I tried your method of destroying the entire gameObject and using OnDestroy() to instantiate new one (which is 2 life), but it will have some weird behaviour like infinite loop of the life(clone) because of gcs.getLife() == 2. Tried using a boolean and integer to check for it but to no avail.