- Home /
NGUI Health SendMessage Has No Receiver
Hi, I'm trying to make a very simple way to pass the health of the player to ngui as a float and adjust the hp bar. The health of the player seems to work, as does adding a float manually to ngui changes the size of the life bar but I can't seem to figure out how to connect the two to check on its own.
I have this script PlayerHealth.cs using UnityEngine; using System.Collections;
public class PlayerHealth : MonoBehaviour {
private int maxHealth = 300;
public int health;
// Use this for initialization
void Start () {
health = maxHealth;
}
// Update is called once per frame
void Update () {
gameObject.SendMessage("AlterHealth", .5f);
}
}
And I have this as VitalBarBasic.cs using UnityEngine; using System.Collections;
public class VitalBarBasic : MonoBehaviour {
private UISlider _slider;
private float _maxWidth;
private float _health;
void Awake() {
_slider = GetComponent<UISlider>();
_maxWidth = _slider.foreground.localScale.x;
}
void Start() {
}
public void Update() {
_slider.sliderValue = _health;
}
public void UpdateDisplay( float x ) {
_slider.sliderValue = x;
}
public void AlterHealth(int newHealth){
_health = newHealth;
}
}
I don't think I have the script actually looking in the right place but I'm a little confused how to structure the "sendmessage" Thanks for any help.
Answer by Sisso · Apr 10, 2013 at 02:13 AM
First. It is not recommended to use SendMessage every frame (Update, LateUpdate, FixedUpdate), it is slower that normal method call. Until you understand more, I recommend avoid SendMessage.
In your code what you are doing is calling asynchronously the method "AlterHealth" of all Components (reference) owned by the game object that owns your script PlayerHealth.
If VitalBarBasic is attached into the same gameObject that PlayerHealth, it should work. If you have 2 gameObjects, you must send a message into another gameObject.
Add a new variable "public VitalBarBasic bar;" into PlayerHealth and resolve its reference in the editor. Then, change your Update to:
// asynchronously call AlterHealth into all bar.gameObject components
bar.gameObject.SendMessage("AlterHealth", .5f);
or
// direct call
bar.AlterHealth(5);
Hello, thank you very much for your response, the Send$$anonymous$$essage was recommended to me awhile ago when I was working in this. I'm a little confused about the "gameObject" This is what my hierarchy looks like if it helps at all. http://puu.sh/2xeU5
GameObject is basically something in your scene. Each item in your hierarchy is a different GameObject. Each GameObject has a name, layer, tag, and a colleciton of Components. (http://docs.unity3d.com/Documentation/Components/class-GameObject.html). Unity3d has many type of components that could be attached in an GameObject. The position, rotation and scale are variables of Transformation component, your script, is a $$anonymous$$onoBehavior Component, and so on.
Search for "unity3d component component-based", like: http://technology.blurst.com/unity-basics-overview/
NullReferenceException: Object reference not set to an instance of an object PlayerHealth.Update () (at Assets/PlayerHealth.cs:18)
That's an error I have now, I'll be honest I think it's a very simple thing I'm not realizing because I don't understand it too well.
Go back to editor and select your Player gameObject. In the inspector, you should have a component tab PlayerHealth with variable bar not assigned. Simple drag and drop your "target" object on it.
It is a simple concept of progra$$anonymous$$g language. I really recommend you to follow some tutorials until you get the basic concepts. Two or tree video tutorials on youtube could sove almost of your problems :P
Oh great no errors! The float isn't being passed though correctly, as the HP bar doesn't change, but the debug log is showing the script is running so I think there is something small messed up. I'll go through it and try to figure it out if I can.