Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SystemIsGod · Apr 09, 2013 at 08:30 PM · c#nguisendmessage

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.

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 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);

Comment
Add comment · Show 5 · 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 SystemIsGod · Apr 10, 2013 at 03:01 AM 0
Share

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

avatar image Sisso · Apr 10, 2013 at 03:32 AM 0
Share

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/

avatar image SystemIsGod · Apr 10, 2013 at 05:30 AM 0
Share

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.

avatar image Sisso · Apr 10, 2013 at 01:09 PM 0
Share

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

avatar image SystemIsGod · Apr 10, 2013 at 04:50 PM 0
Share

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.

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Damage sending with grenades. 1 Answer

Need help working with NGUI progress bar 2 Answers

what should be given as reference for 'public UISlider staminaBar' ? I couldn't find any UISlider in ngui package. 1 Answer


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