- Home /
Destroy an instance of a prefab
Hello, i wrote the following script for the fireworks prefab in my game:
var fireWorksID : int; //This Prefab unique Id to determine which fireworks effect is used!
var speedFactor : float = 10.0;
var secondLimit : float = 4.0;
var target : GameObject;
var gameManager : GameObject;
var gameManagerScript : GameManager;
private var speed : float;
var timer : float;
private var hit : RaycastHit;
private var ray : Ray;
function Start () {
target = GameObject.FindWithTag("target");
gameManager = GameObject.FindGameObjectWithTag("GameManager");
gameManagerScript = gameManager.GetComponent("GameManager");
transform.LookAt(target.transform.position);
}
function FixedUpdate () {
if(Input.touchCount == 1) {
ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
Debug.DrawLine(ray.origin,ray.direction * 10);
if(Physics.Raycast(ray.origin, ray.direction * 10,hit)){
Explode(1);
}
}
}
function Update () {
timer += Time.deltaTime; //Count in seconds
speed = speedFactor * Time.deltaTime;//this may go in update!
transform.Translate(0, 0, speed);
if (timer >= secondLimit) {//Time-Out
Explode(0); //Call Explosion by time out (status = 0)
}
}
function Explode (status : int){
if (status == 1){//detonated
//Call DetonateFirewors() function on game manager with this object's id, status and position
//gameManagerScript.DetonateFireworks(fireWorksID,transform.position,1);
}
else if (status == 0){//timed out
//Call DetonateFirewors() function on game manager with this object's id, status and position
//gameManagerScript.DetonateFireworks(fireWorksID,transform.position,0);
}
else {
//This one should not be called, but just in case it may be managed as a miss or maybe just "neutral"
//gameManagerScript.DetonateFireworks(fireWorksID,transform.position,0);
Debug.Log("Somehow you managed to make a mistake!");
}
Destroy(this.gameObject);
print("Was destroyed!?");
}
My problem is that when in play mode i touch the prefab all the active clones are destroyed too, i want to destroy only the one i touched, however when they "timeout" they actually destroy only themselves... I hope you can help me , thanks :)
Try ins$$anonymous$$d of setting timer variable in the editor, do it in Start(), make it a private variable and have another to set it.
In The Explode function, you should change the structure of your if else statements
if(status==0)
{
game$$anonymous$$anagerScript.DetonateFireworks(fireWorksID,transform.position,0);
}
if(status==1)
{
game$$anonymous$$anagerScript.DetonateFireworks(fireWorksID,transform.position,1);
}
else if(status!=0) //'else' status is not 1,'if' status is not 0
{
Destroy(this.gameObject);
print("Was destroyed!?");
}
Are you destroying the object in DetonateFireworks? You should provide that code if you are destroying them there. I dont see anywhere you destroy in this code other than the "mistake" else statement.
Answer by kolban · May 08, 2012 at 08:56 PM
In your FixedUpdate() code you are casting a ray and asking did I hit AN Object. If the answer is yes, then you explode. I think what you want to do is cast a ray and ask did I hit ME and if yes, then Explode.
It also seems to me that you are performing input processing for each instance of the GameObject ...
I can't help but feel that it would be better to have a separate (empty) Game Object to own your input processing so that the input processing is done just once. In THAT input processing, you would cast the Ray once and then if it hit an object, you would ask THAT object to explode as opposed to having all objects do input processing and ask if THEY should explode.
I think what you say about the ray casting an object is what i should do, however how do i check if the ray hits that particular instance??? Also, How can i implement that separate input processing GameObject? Sorry if sound so basic, its because im not a programmer
Thanks again for your time!
Your answer
Follow this Question
Related Questions
How to destroy a game object within an instance of prefab 1 Answer
Instantiated object is destroyed in scene, but still logs as existing in console. 1 Answer
Destroy(clone) not destroying the instantiated prefab 1 Answer
How to refer to a collision between clones of the same prefab? C# 1 Answer
Problem destroying cloned prefabs 1 Answer