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 EliteHedgehog56 · Jan 17, 2018 at 07:39 AM · activatedeactivate

problem with enable/disable on health script

so I have a modified version of my health script that a user in the community had edited for me in a different question. The script is pretty much fine but I am having issues with the enable and disable parts of the scritpt. What I wanted to do is when the player dies it will temporarily disable and when the player respawns it will enable again. However when the player dies nothing happens the health variable continues to take damage and end up on a negative number. Is there a better way to achieve the same or similar results?

here is the script

  var PointCube: Transform;
  var spawneffect: Transform;
  var deatheffect: Transform;
  var SpawnPoints: Transform[];
  private var SpawnPoint: Transform;
  
  //Respawn timer for the coroutine
  var respawnTime: float = 3;
  
  var respawn: boolean = false;
  
  //This is the players current health
  var health: float = 100;
  
  //The max health the player is spawned with
 
  var healthText: UI.Text;
  
  //At start set the players health to maxHealth
  function Start() {
      health = 100;
  }
  
  function Damage(dmg: float) {
      health -= dmg;
  }
  
  function Update() {
      if (healthText) {
          healthText.text = health.ToString();
              // If the players health is zero start the respawn function
          if (health <= 0) {
  yield Respawn();
          }
      }
  }
  
  //This is a coroutine. Basically meaning it has a special wait timer function
  function Respawn() {
      //temporarily disable player
      gameObject.SetActive (false);
 
 
      Instantiate(deatheffect, transform.position, transform.rotation);
      Instantiate(PointCube, transform.position, transform.rotation);
  
      //Wait for the respawnTimer. (This is the special wait timer I was speaking of)
      yield WaitForSeconds(respawnTime);
  
      //Find a random spawn point
      var spawnPointID: float = Random.Range(0, SpawnPoints.length);
  
      //Set the player at that random spawn point
      transform.position = SpawnPoints[spawnPointID].position;
  
      //enable player
      gameObject.SetActive (true);
 
 
      Instantiate(spawneffect, transform.position, transform.rotation);
  
      var audio: AudioSource = GetComponent.<AudioSource>();
      audio.Play();
      audio.Play(44100);
  
      //change this to set float not add to it
      health = 100;
  }

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
Best Answer

Answer by fafase · Jan 17, 2018 at 07:45 AM

Make sure the value is never negative:

 function Damage(dmg: float) 
 {
       health -= dmg;
      if (health < 0) { health = 0; }
 }

But are you sure your code works? Coz you are disabling the game object so the coroutine won't run.

You 'd need to disable the rendering and collider only

Comment
Add comment · Show 3 · 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 EliteHedgehog56 · Jan 17, 2018 at 08:01 AM 0
Share

@fafase ahh I see, the same thing the other guy on my last question said I'll give that a try but is there a way I can disable the player's camera child object from this script so it would hide the player's camera so the game can display the spectating camera or is there no need to do this ?

avatar image fafase EliteHedgehog56 · Jan 17, 2018 at 08:44 AM 0
Share

I'd say you don't need to disable the camera. Just unparent it from the player, then place it where it should be. When restarting your player, parent it back where it was.

 void Death(){
      Camera.main.transform.parent = null;
      Camera.main.transform.position = spectatingposition;
 }
 
 void Rebirth()
 {
      Camera.main.transform.parent = player.transform;
      Camera.main.transform.position = eyePosition;
 }

avatar image EliteHedgehog56 · Jan 18, 2018 at 03:39 AM 0
Share

@fafase

I think I found the problem. I think it has something to do with the wait event, because I want it to wait 3 seconds before continueing the rest of the script but it seems to be negating the rest of the script

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

73 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 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 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 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 avatar image

Related Questions

Activate Game object when in Camera view and deactivate when out of camera view. 2 Answers

Most effective way to get deactivated children? 1 Answer

How to activate the child of an object without declaring a public transform? 1 Answer

How To Disable A Prefab? 2 Answers

ReActivate object with null movement ?? 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