- Home /
Problems With Health pack Script
I'm placing this code
using UnityEngine;
using System.Collections;
public class HealthTrigger : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
GameObject Player = GameObject.Find("Player");
PlayerHealth playerhealth = Player.GetComponent<PlayerHealth>();
playerhealth.curHealth += 10;
Destroy (gameObject);
audio.Play();
}
}
on a heart container in my adventure game, it gets the value of curHealth and is supposed to add ten health disapeer and play a sound, any Help? P.S. Yes Heart container is a trigger
There is a "101 010"-button above your edit-window. It will format the selected text as code. All it does is this:
Indent every line by 4.
add a black line before and after the code-block.
This will make the text being displayed as code.
Answer by Robinmtb · Jan 15, 2012 at 12:35 AM
Hi!
First I'll repost your code with breaks so it's easier to read :D
using UnityEngine;
using System.Collections;
public class HealthTrigger : MonoBehaviour {
//Use this for initilization
void Start () { } // Update is called once per frame
void Update () { }
void OnTriggerEnter(Collider other) {
GameObject Player = GameObject.Find("Player");
PlayerHealth playerhealth = Player.GetComponent();
playerhealth.curHealth += 10;
Destroy (gameObject);
audio.Play();
}
}
Ok, first off, the reason your audio isn't playing is because you destroy the gameObject before you try to play it. A good solution to this is:
playerhealth.curHealth += 10;
renderer.enabled = false; //Objects mesh isn't visible
audio.Play();
yield WaitForSeconds (audio.clip.length); //Waits until audio is finished
Destroy (gameObject);
Now for the problem with the health not being added.
I think the reason for this could be that you are working with a local instance of the component. Try writing a method in PlayerHealth like:
void AddHealth(int health){
curHealth += health;
}
and see if that works.
Are you sure you're getting the correct component? I'm not used to C# so I haven't seen GetComponent used like that without anything in the () before :)
Good Luck! I hoped I was to some help!
Robin
Thats perfect thank you, one question how did you get the code to format like that
Darn it I knew it was probably something simpe like that, Thanks Robinmtb
@lijrobert: Oh and use "add new comment" if you want to comment ;). Answers should always answer the question. If you figured out the answer yourself, feel free to post an answer yourself.
I've converted you answer into a comment. I see you already posted another comment, your "answer" was stuck in the moderation queue to prevent spam. As soon as you have 16+ karma you can post directly.
@Bunny83 Thank you for the help, I'll remember, I just joined so have to figure some of the stuff out :D
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# add helth over time 1 Answer
c# help please 1 Answer