Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 davidflynn2 · Nov 18, 2013 at 06:26 PM · c#respawn

Respawn Script Problem

The problem with the below code is that it only works once and I want it to work every time the ship dies.

I have a health script on my ship that has a the fallowing code for the resawn. This script is longer but this is the part that does it. destroyed is called.

     void destroyed()
     {
             Destroy (GameObject.Find("Guns"),1);
             animation.wrapMode = WrapMode.Once;
             animation.wrapMode = WrapMode.Once;
             animation["LeftWing"].wrapMode = WrapMode.Once;
             animation["LeftWing"].layer = 3;
         
             animation.Play ("LeftWing");
             Destroy (GameObject.Find("Left_Wing"),3);
         
             animation.wrapMode = WrapMode.Once;
             animation["RightWing"].wrapMode = WrapMode.Once;
             animation["RightWing"].layer = 4;
         
             animation.Play ("RightWing");
             Destroy (GameObject.Find("Right_Wing"),3);
         
             animation.wrapMode = WrapMode.Once;
             animation["RightThruster"].wrapMode = WrapMode.Once;
             animation["RightThruster"].layer = 5;
         
             animation.Play ("RightThruster");
             Destroy (GameObject.Find("Right_Engine"),3);
         
             animation.wrapMode = WrapMode.Once;
             animation["LeftThruster"].wrapMode = WrapMode.Once;
             animation["LeftThruster"].layer = 6;
         
             animation.Play ("LeftThruster");
             Destroy (GameObject.Find("Left_Engine"),3);
         
             animation.wrapMode = WrapMode.Once;
             animation["WindowBlowoff"].wrapMode = WrapMode.Once;
             animation["WindowBlowoff"].layer = 7;
         
             animation.Play ("WindowBlowoff");
             Destroy (GameObject.Find("Window"),3);
             
             animation.wrapMode = WrapMode.Once;
             animation["WindowFrameBlowoff"].wrapMode = WrapMode.Once;
             animation["WindowFrameBlowoff"].layer = 8;
         
             animation.Play ("WindowFrameBlowoff");
             Destroy (GameObject.Find("Window_Frame"),3);
         
             WingDamage.SetActive(false);
             WingDamage2.SetActive(false);
         
             
             Destroy (GameObject.Find("Star Blaster"),4);
             Destroy (GameObject.Find ("Star Blaster(Clone)"),4);
         
         
             GameObject varGameObject = GameObject.Find("Main Camera"); // Finds the main ship
            varGameObject.GetComponent<CameraFollow>().enabled = false; // Finds the fly script and turns it off if we die
         
             curShipHealth = 0;
             texture.GetComponent<AddDamage>().thirdDamage = true; // Finds the fly script and turns it off if we die
             MassDamage.SetActive(true);
             explosion.SetActive(true);
             player.GetComponent<ShipThrusterControle>().level3 = true; // Finds the fly script and turns it off if we die
         
 
 //            texture.GetComponent<AddDamage>().firstDamage = false; // Finds the fly script and turns it off if we die
 //            texture.GetComponent<AddDamage>().secondDamage = false; // Finds the fly script and turns it off if we die
 //            texture.GetComponent<AddDamage>().thirdDamage = false; // Finds the fly script and turns it off if we die
 //            texture.GetComponent<AddDamage>().Dead = false; // Finds the fly script and turns it off if we die
 //        
 //            texture.GetComponent<AddDamage>().Repair = true; // Finds the fly script and turns it off if we die
 //            texture.GetComponent<AddDamage>().Repair = false; // Finds the fly script and turns it off if we die
             
         curShipHealth = 100;
             StartCoroutine(Respawn());
             
         
     }
     IEnumerator Respawn()
     {
         
         
         yield return new WaitForSeconds (3);
         myShip.SetActive (false);
         GameObject varGameObject = GameObject.Find("Main Camera"); // Finds the main ship
         Spawn respawn = varGameObject.GetComponent<Spawn>(); // Finds the fly script and turns it off if we die
         respawn.enabled = true;
         respawn.dead = true;
         
 
             
     }







This turns on a script I have on my main camera that I am trying to use to respawn the ship.

This code:

 using UnityEngine;
 using System.Collections;
 
 public class Spawn : MonoBehaviour 
 {
     public Transform ship;
     public Transform location1; // The teleport after death location
     private Transform newShip;
     
     public bool dead = false;
     
     void Start ()
     {
         Dead();
     }
     
     void Dead()
     {
         if(dead)
         {
             GameObject clone = Instantiate (ship, location1.position, location1.rotation)as GameObject;
             newShip = GameObject.FindWithTag("Player").transform;
             
             GameObject varGameObject = GameObject.Find("Main Camera");
              CameraFollow cameraFollow = varGameObject.GetComponent<CameraFollow>();
             ShipHealth health = varGameObject.GetComponent<ShipHealth>();
             
             cameraFollow.enabled = true;
             cameraFollow.target = newShip;
             
             dead = false;
 
              varGameObject.GetComponent<Spawn>().enabled = false; // Finds the fly script and turns it off if we die
             
         }
         
     }
 
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tomer-Barkan · Nov 18, 2013 at 06:36 PM

You're calling Dead() in your camera only once, in the Start() method. Try using Update() instead of Start(), so it will check the dead variable every frame and not just once:

 void Update() {
     Dead();
 }
Comment
Add comment · Show 2 · 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 davidflynn2 · Nov 18, 2013 at 06:48 PM 0
Share

I have been playing with that this is what the new script on the camera looks like and its works the same :

 using UnityEngine;
 using System.Collections;
 
 public class Spawn : $$anonymous$$onoBehaviour 
 {
     public Transform ship;
     public Transform location1; // The teleport after death location
     private Transform newShip;
     
     public bool dead = false;
 
     
     void Update()
     {
         if(dead)
         {
             GameObject clone = Instantiate (ship, location1.position, location1.rotation)as GameObject;
             newShip = GameObject.FindWithTag("Player").transform;
             
             GameObject varGameObject = GameObject.Find("$$anonymous$$ain Camera");
              CameraFollow cameraFollow = varGameObject.GetComponent<CameraFollow>();
             ShipHealth health = varGameObject.GetComponent<ShipHealth>();
             
             cameraFollow.enabled = true;
             cameraFollow.target = newShip;
             
             dead = false;
             
         }
         
     }
 
 }
avatar image Tomer-Barkan · Nov 18, 2013 at 06:59 PM 0
Share

You got a lot of logic in your code, and something might be messing things up... Start by putting a Debug.Log just before calling StartCoroutine(), one before and one after the yield statement. Also add another Debug.Log inside the if(dead) statement. Check out which of them are being printed.

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How to undo destroying a component, after 5 seconds. 0 Answers

Distribute terrain in zones 3 Answers

With my respawn script, Player can't move for about 5 seconds. 0 Answers

Finding the player that walked into a ray? 1 Answer


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