• 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 Djspun · Sep 30, 2013 at 09:35 PM · movehealthnew

c# not going to destination on 0 hp

my script it is supposed to spawn an invisible portal on 0 playerhealth that takes me to dz_Default but nothing happens on 0 playerhealth

 /// 
 /// PlayerHealth.cs
 /// Display the players health in game
 /// 
 /// Attach this class to your player character
 /// 
 using UnityEngine;
 using System.Collections;
 
 public class PlayerHealth : MonoBehaviour {
 
     private const string DEFAULT_DROP_ZONE_NAME = "dz_Default";
     public GameObject destination;
     
     public int maxHealth = 100;
     public int curHealth = 100;
     
     
     
     public float healthBarLength;
 
     // Use this for initialization
     void Start () {
         healthBarLength = Screen.width / 2;
     }
     
     // Update is called once per frame
     void Update () {
         AddjustCurrentHealth(0);
     }
     
     void OnGUI() {
         GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
     }
     
     public void AddjustCurrentHealth(int adj) {
         curHealth += adj;
         
         if(curHealth < 0)
             curHealth = 0;
         
         if(curHealth > maxHealth)
             curHealth = maxHealth;
         
         if(maxHealth < 1)
             maxHealth = 1;
         
         if(curHealth <= 0)
 {
        
             destination = GameObject.Find( DEFAULT_DROP_ZONE_NAME );
             
 }
 
         healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
 
     }
         public void OnTriggerEnter( Collider other ) {
         if( other.transform.CompareTag("Player") )
             other.transform.position = destination.transform.position;
         
     }    
         
 }
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 getyour411 · Sep 30, 2013 at 11:52 PM

The only thing you are doing on playerHealth = 0 is setting the destination variable to some value, since you have the move code within an OnTriggerEnter(). After this

  destination = GameObject.Find( DEFAULT_DROP_ZONE_NAME );

add this

  transform.position = destination.transform.position;
Comment
Add comment · Show 1 · 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 Djspun · Oct 01, 2013 at 01:43 AM 0
Share

thanks it works

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

14 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

Related Questions

Multiple Cars not working 1 Answer

c#help reversing a script 1 Answer

c# destroy gameobject on 0 hp 2 Answers

Drag And drop objects mouse 2 Answers

c# add/move lines of text in GUI box 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges