Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DFiable · Jul 23, 2011 at 05:03 AM · destroyclonerespawn

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.

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DFiable · Jul 23, 2011 at 05:10 AM 0
Share

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?

avatar image DFiable · Jul 23, 2011 at 05:55 AM 0
Share
 // 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);
         }
 }
avatar image Herman-Tulleken · Jul 23, 2011 at 09:33 AM 0
Share

Hey T-Roy. You can format code by using the button with 0s and 1s on it.

avatar image DFiable · Jul 23, 2011 at 10:45 AM 0
Share

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.

avatar image coastwise · Jul 23, 2011 at 01:23 PM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
  }
}
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DFiable · Jul 23, 2011 at 11:01 PM 0
Share

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

avatar image DFiable · Jul 23, 2011 at 11:30 PM 0
Share

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?

avatar image aldonaletto · Jul 24, 2011 at 12:30 AM 0
Share

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;

avatar image DFiable · Jul 24, 2011 at 01:29 AM 0
Share

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?

avatar image aldonaletto · Jul 24, 2011 at 01:46 AM 0
Share
  • 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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges