• 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 bomerman104 · Dec 06, 2019 at 01:53 AM · nullreferenceexceptionsliderexception

NullReferenceException: Object reference not set to an instance of an object Player.Start () (at Assets/Scripts/Player.cs:29)

So I cannot for the life of me figure out what I'm doing wrong here, I have looked everywhere and haven't found anything to help me.

Essentially I have two sliders, one for Health the other for Mana. no matter ho many different combinations I try I can't seem to be able to do anything with the sliders as I get the error above.

Here is my code.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Player : MonoBehaviour
 {
     public Slider HealthBar;
     public Slider ManaBar;
 
     private Slider Health;
     private Slider Mana;
 
     private float MaxHealth = 100;
     private float MaxMana = 40;
     private float CurrentHealth = 100;
     private float CurrentMana = 40;
     private float Level = 1;
     private float Armor = 5;
     private float MagRes = 0;
     private float Str = 10;
     private float Speed = 15;
 
     
 
     // Start is called before the first frame update
     void Start()
     {
         Health = HealthBar.GetComponent<Slider>();
         Mana = ManaBar.GetComponent<Slider>();
 
         CurrentHealth = MaxHealth;
         CurrentMana = MaxMana;
         Health.value = CurrentHealth;
         Mana.value = CurrentMana;
     }
 
     // Update is called once per frame
     void Update()
     {
         CurrentHealth -= Time.deltaTime;
         Health.value = CurrentHealth;
         CurrentMana -= Time.deltaTime;
         Mana.value = CurrentMana;
     }

and here is a screenshot of the Inspector as well. alt text

capture.png (135.3 kB)
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 Bunny83 · Dec 06, 2019 at 02:23 AM

In this line:

 Health = HealthBar.GetComponent<Slider>();

you try to get a component Slider on the gameobject which the Slider component is attached to that you wanted to assign in HealthBar. However in your first Player script you did not assign anything to that variable as we can see in your screen shot. So it's pretty obvious that you can not do anything with the object referenced by HealthBar if you did not put any reference in that variable.


Just in case you did not notice you have attached your Player script two times to this gameobject. Once before your Rigidbody2d component and once after. You have assigned your sliders in your second instance but not in the first. You probably want to remove one of them.


Apart from that obvious mistake your GetComponent line(s) are completely pointless. Your variable HealthBar is already of type Slider so when you drag a gameobject to that variable you already assigned a reference to the Slider component. Calling GetComponent on that reference (if it is setup) it completely redundant.

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 bomerman104 · Dec 06, 2019 at 02:29 AM 0
Share

It's always the little things I swear.

Thank you for pointing that out to me.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

NullReferenceException in follow function 0 Answers

Null Reference Exception 0 Answers

Can't drag slider onto inspector 1 Answer

NullReferenceException Error 6 Answers

FindWithTag returns NullReferenceException 0 Answers


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