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 GKdev1980 · Apr 09, 2015 at 08:29 AM · uinullreferenceexceptionslider

Unity 4.6 UI Slider NullReferenceException

I'm trying to migrate from NGUI to the new native UI system, I'm getting a strange NullReferenceException in my slider..

First of all, I use my slider as a healthbar, so I've deleted the handle object and disabled "interactable".

I've attached a script to the slider object, this script

using UnityEngine; using UnityEngine.UI; using System.Collections;

public class HealthScript : MonoBehaviour {

 public static bool eatNow;

// public GameObject pHealthBar; Slider healthSlider;

 float health;

// float healthValue;

 void Start(){
     health = 100f;
     eatNow = false;
     healthSlider = GetComponent<Slider>();

// healthValue = healthSlider.value; }

 void Update(){

// health = health - 0.05f; healthSlider.value = health; health = health - 0.05f; Debug.Log (health);

     if (eatNow == true){
         eatNow = false;
         HealthRise();
     }
 }


I get an exception in a line in the update

 healthSlider.value = health;





I've tried declaring healthSlider as public, but it still throws the exception..

What's wrong with my code? I've managed to resolve nullreferenceexceptions errors in the past, I'm stuck with this one...

Comment
Add comment · Show 6
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 Nerevar · Apr 09, 2015 at 08:35 AM 0
Share

A simpler way would be to use a UI Image component and change the image Type by "Filled" set Fill $$anonymous$$ethod vertical or horizontal and the origin depending on what you need (you don't really need the whole slider system). Fill amount will allow you to animate your health bar.

avatar image GKdev1980 · Apr 09, 2015 at 09:03 AM 0
Share

I'm aware of that, but I went through the official Unity tutorials and I'm pretty sure they mentioned that the slider could be used for this purpose too..

If I'm unable to resolve this I will probably switch to the way you proposed, but it would be nice to know what exactly is causing the exception and keep that in $$anonymous$$d for future reference...

avatar image Nerevar · Apr 09, 2015 at 12:30 PM 0
Share

Ok well then, give us the full error log and line if there is one.

$$anonymous$$aybe the slider is trying to access the hanlder you deleted.

avatar image GKdev1980 · Apr 09, 2015 at 12:45 PM 0
Share

The log file is huge..

The only reference to the Healthscript is this

 NullReferenceException: Object reference not set to an instance of an object
   at HealthScript.Update () [0x00000] in H:\_Unity Projects_\project\Assets\Scripts\HealthScript.cs:25 


, which keeps repeating (expected, since it's inside the Update function)

avatar image GKdev1980 GKdev1980 · Apr 09, 2015 at 12:47 PM 0
Share

Sorry, I accidentally replied as a new answer ins$$anonymous$$d of just adding a new comment...

avatar image Blackup · Apr 09, 2015 at 01:43 PM 0
Share

I think the new GUI objects are not exactly the same as normal GameObjects. For one, they have a different transform class... Because of this, I suspect that maybe your script isn't recognizing its owner as a standard Game object...

Try embedding the slider control in an ordinary parent Game object ins$$anonymous$$d. Then attach your script to the Parent object in s$$anonymous$$d. Then use a find method to locate and assign the Slider object... e.g. "GameObject temp = GameObject.Find("Volume Slider");" or "GameObject temp = GameObject.transform.Find("Volume Slider").gameObject;" or something like that...

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DiegoSLTS · Apr 09, 2015 at 01:28 PM

The line where you get the Slider component is probably returning null, make sure the game object with the HealthScript component attached also has the Slider component attached.

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 GKdev1980 · Apr 09, 2015 at 01:49 PM 0
Share

I figured out what was causing the problem. I did have the script attached to a gameobject with a slider, but I had accidentally attached it to a second empty GameObject.

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

19 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't drag slider onto inspector 1 Answer

Lagging/unmoving slider 0 Answers

How to Make a Vertical-Scroll Number Input Field, i.e. < > Value Amount 0 Answers

Slider with max value in the middle 0 Answers

Collision Updating UI Slider only once. 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