- Home /
How do you destroy respawned clones?
var respawnPrefab : GameObject;
function OnTriggerEnter(myTrigger: Collider){
if(myTrigger.gameObject.name == "tum1-2"){//tum1-2 is a wall
//respawns at the location of a game object tagged Respawn in the middle of the game.
var respawns = GameObject.FindGameObjectsWithTag ("Respawn");
for (var respawn in respawns) {
Instantiate(respawnPrefab,
respawn.transform.position, respawn.transform.rotation);
}
Time.timeScale = 1;
}
}
This is my respawn script and it works well. When my collider gameObject hits the wall the player dies and then respawns back at the begining of the level. If the player crashes and dies again using the instantiated cloned gameObject
, a new cloned object appears in my hierarchy called "FPS(clone)(clone)". The first one was called "FPS(clone)".
I'm trying to write a script with the expression == null
and !=null
but I can't figure it out. I'm trying to figure out how to only have one instance of the cloned gameObject
at anyone time. Can anyone help me with this?
I've read through many of the answers but I haven't been able to apply it to my script which is straight out of the Unity API reference under respawn.
How do I destroy the previous cloned game object after respawning again and again? Can someone help me modify my script?
Thanks.
I forgot to say, I also have a Destroy() script attached to the game object(player) that works fine the first time. But it only works once. Any suggestions?
// Destroy everything that enters the trigger
function OnTriggerEnter(myTrigger: Collider){
if(myTrigger.gameObject.name == "tum1-2"){
//Detroys First Preson Controller-the 1 is the delayed time before it is destroyed'
Destroy(gameObject.Find("FPS"),1);
}
}
Hey T-Roy. You can format code by using the button with 0s and 1s on it.
Thank you, Herman. One other thing. When I destroy the first clone the other clones are also destroyed probably because they are an instance. Any suggestions. thanx.
From your description of spawning a "FPS(clone)(clone)" it seems like you might not have an actual prefab in your respawnPrefab variable. Try to drag one over from your Assets pane, not your Hierarchy pane.
Answer by aldonaletto · Jul 23, 2011 at 05:52 PM
This is somewhat confusing. Let's change the logic a little: the Respawn object will contain the prefab reference in playerPrefab, and will respawn it when its function RespawnPlayer is called. The player will suicide upon entering the death wall trigger, but will call RespawnPlayer before leaving this world.
I'm supposing that:
- The wall named "tum1-2" is the trigger;
- The player must die when touching it;
- Once dead, the player must respawn at a fixed point tagged "Respawn"
1- Save this script as RespawnScript.js, attach it to the Respawn object, and drag the player prefab to playerPrefab at the Inspector:
var playerPrefab: GameObject; // drag the player prefab here
function RespawnPlayer(){ Instantiate(playerPrefab, transform.position, transform.rotation); }
2- Attach this script to the player:
function OnTriggerEnter(hit:Collider){ if (hit.name == "tum1-2"){ // if player hit the wall... Destroy(gameObject); // it suicides // get the object tagged "Respawn" var objRespawn = GameObject.FindWithTag("Respawn"); // get the script RespawnScript.js var script: RespawnScript = objRespawn.GetComponent(RespawnScript); script.RespawnPlayer(); // respawn a new player } }
thanx aldonaletto. O$$anonymous$$ I tried it and it works once, but If the player runs into the wall five times and dies five times it respawns five times. The new clones don't die. Does this have something to do with the way I have created my prefab. Currently I created a (first person controller with children prefab, main camera and a sphere)I named it FPS. Almost all of my scripts are attached to it. Any ideas. thanx
No. I rebuilt the prefab with my First Preson Controller and children, draged it into the scene and reset the positions. It still makes multiple clones. Does the respawn of my First person controller have to be in an empty game object or something? Why does the second clone not die when it hits the wall? Remember I tried destroying the original clone and it kills all of the clones. Does this make sense?
It should work fine. In your original script, the clones were not killed because their names were "FPS (clone)", "FPS (clone)(clone)" etc. and you were killing only those named "FPS". In this case it should work, because is the player who kills himself when touches the trigger - and all clones should do the same.
Let's do it step by step:
1- Drag the player prefab to the scene;
2- Attach the script #2 to it and click Apply to update the prefab;
3- Drag the player prefab (not the player you've placed in scene!) to the playerPrefab variable in the Respawn object; if Respawn has its own prefab, click Apply to update it too;
I did that exactly. I attached the script before I created the prefab. Then I created a prefab with the script attached. If I do it after, doesn't that destroy the prefab connection? In any event, I did it both ways and I still get FPS(Clone), FPS(Clone)(Clone) etc... everytime the Sphere hits the wall. Do you see anything else that I might be doing wrong?
Don't $$anonymous$$d if Unity warns you will lose the prefab: do the modifications you need and drag the edited object to the prefab.
Have you removed all the old scripts? $$anonymous$$aybe some of them are still active and doing this strange thing.
Unity only appends "(clone)" to the name of an instantiated object if the original is a scene object - if the original is a prefab it just repeats the original name.
Your answer
Follow this Question
Related Questions
Respawn question unanswered 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
how to spawn and destroy 2 Answers
keeping the xp after death 2 Answers
Null Reference exception help 2 Answers