• 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 /
This question was closed Nov 20, 2014 at 03:44 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Wizardman290 · Nov 19, 2014 at 03:24 AM · c#respawnhealth

My script to kill the player and respawn, does nothing?

I've been trying to get answers from on here for a day or 2 now, and I cannot seem to find one. Each answer = new question. I t$$anonymous$$nk I am getting closer, before I had an error, now it just doesn't do anyt$$anonymous$$ng. (I probably did somet$$anonymous$$ng stupid somewhere) I am very new to coding, so don't expect me to understand a lot (Do you know any good tutorial series?). I basically want to get rid of the PlayerMovement, and then 2 seconds later get rid of Mesh, BoxCollider, if Health <= 0, (In case it goes negative or somet$$anonymous$$ng). And then, after five seconds, I want all three of those to come back. And then, Health = 100. I thought I had the code right, but when health = 0, not$$anonymous$$ng happens. Thanks in advance for the help (And possibly a good series to watch on youtube for c# coding)

Here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class AttributeDeclaration : MonoBehaviour {
     #region Public ints
     public int Health = 100;
     public int Magic = 100;
     public int Strength = 100;
     public int Stamina = 100;
     public int Defense = 100;
 
     //Maximum attributes
     public int MaxHealth = 100;
     public int MaxMagic = 100;
     public int MaxStrength = 100;
     public int MaxStamina = 100;
     public int MaxDefense = 100;
 
     //Testing Stuff
     public int DamageAmount = 20;
 
     //---------------------------------------------------------------------------------------------
     #endregion
 
     public PlayerLevelDeclaration PT;
     public PlayerMovement PM;
     public HealthPotion HP;
 
     //----------------------------------------------------------------------------------------
     void Awake(){
         newPos = transform.position;
     }
     //-------------------------
     // Use t$$anonymous$$s for initialization
     void Start () {
     }
     //-----------------------------------------------------------------------------------------
     // Update is called once per frame
     void Update () {
         PT = gameObject.GetComponent<PlayerLevelDeclaration>();
         HP = gameObject.GetComponent<HealthPotion>();
         PM = gameObject.GetComponent<PlayerMovement>();
 
         TempDamageHealth();
         TempDamageStamina();
         TempDamageMagic();
         TempDamageStrength();
         TempDamageDefense();
 
         DisallowNegative();
 
         KillPlayer();
     }
     //----------------------------------------------------------------------------------------
     void DisallowNegative(){
         if(Health <= 0)
         {
             Health = 0;
         }
         if(Stamina <= 0)
         {
             Stamina = 0;
         }
         if(Magic <= 0)
         {
             Magic = 0;
         }
         if(Strength <= 0)
         {
             Strength = 0;
         }
         if(Defense <=0)
         {
             Defense = 0;
         }
     }
     //-----------------------------------------------------------------------------------------
 
     public int TempDamageHealth()
     {
 
         if(Input.GetKeyDown(KeyCode.Keypad1))
         {
             Health = Health - DamageAmount;
         }
         return Health;
     }
     //-------------------------------------------------------------------------------------
     public int TempDamageStamina()
     {
         
         if(Input.GetKeyDown(KeyCode.Keypad2))
         {
             Stamina = Stamina - DamageAmount;
         }
         return     Stamina;
     }
     //--------------------------------------------------------------------------------------
     public int TempDamageMagic()
     {
         
         if(Input.GetKeyDown(KeyCode.Keypad3))
         {
             Magic = Magic - DamageAmount;
         }
         return Magic;
     }
     //------------------------------------------------------------------------------------------
     public int TempDamageStrength()
     {
         
         if(Input.GetKeyDown(KeyCode.Keypad4))
         {
             Strength = Strength - DamageAmount;
         }
         return Strength;
     }
     //----------------------------------------------------------------------------------
     public int TempDamageDefense()
     {
         
         if(Input.GetKeyDown(KeyCode.Keypad9))
         {
             Defense = Defense - DamageAmount;
         }
         return Defense;
     }
 //--------------------------------------------------------------------
     Vector3 newPos;
     bool IsAlive = true;
     IEnumerator KillPlayer(){
     if(Health <= 0)
         {
             IsAlive = false;
             Health = 0;
             //Hopefully t$$anonymous$$s makes it so the player can't move as soon as Health = 0, but doesn't disappear and lose 
             //collision until after 2 seconds. Then, 5 seconds later, the player's position is set to 0, 0, 0, and 
             //He regains movement, reappears, and regains collision.
 
             //Kill Player
             PM.enabled = false;
             yield return new WaitForSeconds(2f);
             renderer.enabled = false;
             collider.enabled = false;
             PM.enabled = false;
                     
             //Wait 5 seconds
             yield return new WaitForSeconds(5f);
 
             //Set position code;
             Vector3 PlayerSpawn = new Vector3(0f , 0f , 0f);
             newPos = PlayerSpawn;
             transform.position = newPos;
             //Rest
             renderer.enabled = true;
             collider.enabled = true;
             PM.enabled = true;
             IsAlive = true;
             Health = MaxHealth;
 
 
         }
         yield return IsAlive == true;
     }
 
 }
 
Comment
Add comment · Show 2
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 Sir-Irk · Nov 19, 2014 at 06:28 AM 0
Share
avatar image Wizardman290 · Nov 20, 2014 at 03:42 AM 0
Share

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by HarshadK · Nov 19, 2014 at 06:25 AM

You need to call your KillPlayer() coroutine using either of the methods provided by @Sir-Irk.

I would also suggest you to not call your KillPlayer() coroutine from Update rather call it only once when the health is zero. You can do it from the disallowNegative() function as below:

 void DisallowNegative(){
  if(Health <= 0)
  {
      Health = 0;
      StartCoroutine(KillPlayer());  // Or you can also use StartCoroutine("KillPlayer");
  }
Comment
Add comment · 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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Help - Respawn or Reset 1 Answer

Respawning without creating Clones 2 Answers

Problem with making a player respawn in unity 2D (C#) 4 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