- Home /
Health does not update in this script, why?
Ok guys, here's my recent script based on another one from the Digital Tutors website, but something is going wrong. I tagged my toolboxes as "toolbox" and the i added this script to my player, but neither the life is being updated, and the toolbox does not disappear.. I can't actually figure out what am i doing wrong.
Here's my script people:
var health:float = 100; // variable to track our health value var startHealth:float; // var to store the max health from the inspector function Update () { //health = health-1; if (health < 0) { // if we are out of health Die(); // run the Die function } }
function Awake() { // when script is initialized, set the startHealth to the health from the inspector startHealth = health; }
function OnGUI () { // every GUI update GUI.Box (Rect ( 200,200,150,30), "Player Health: " + Mathf.Round(health)); // create a text box with the health in there }
function OnTriggerEnter(other : Collider) {
if (collider.tag == "toolbox") { // if the trigger tag is a toolbox if(health<=80) { health = health+20; Destroy(other.gameObject); } else { health=100; } } }
function Die() { // when dead, do the following transform.position = Vector3(1.060952,1.071588,8.590559); // set the position to the starting location transform.rotation = Quaternion.Euler(0,180,0); // reset the rotation health = startHealth; // reset the health }
Hope you can help me people. This is for a project that i have to deliver after tommorow. :\ Kind of desperate here. Thanks a lot.
I don't know why is my code unformated here but i hope you can understand it.
Answer by delstrega · Jul 10, 2012 at 01:38 PM
My guess would be that you are checking the wrong collider for the "toolbox" tag. Since you posted the player script,
collider.tag
will return the tag of the player's collider, which will never be "toolbox".
Try to check
if(other.tag == "toolbox")
instead.
Hope that helps.
IT DOES!!!... How dumb was i for not notice that... $$anonymous$$y character is on the go... Thanks delstrega...
No problem at all. Sometimes it's logical bugs like this that are the hardest to find! :D