Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by 10chaos1 · Apr 03, 2016 at 04:05 PM · c#null reference exception

null reference error on player ui script

hello there its been a while since i posted here ok so my problem is that my playerui script is giving me a NRE on lines 39,65 and 66 which is strange because as you will see in the picture shown there are set correctly in the inspector what am i doing wrong here is the code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class PlayerUI : MonoBehaviour
 {
 
     public Image SleepIcon;
     public Image ToiletIcon;
     public Image Fader;
     public Image HealthBar;
     public Image HungerBar;
     public static Survivor S;
     public static PlayerStats P;
     Animator FadeAnim;
     Animator SleepyAnim;
     Animator NeedsToGoAnim;
     int FadeHash = Animator.StringToHash ("fade");
     int SleepyHash = Animator.StringToHash ("sleepy");
     int NeedsToGoHash = Animator.StringToHash ("needstogo");
 
 
 
 
     // Use this for initialization
     void Start ()
     {
 
         FadeAnim = Fader.GetComponent<Animator> ();
         SleepyAnim = SleepIcon.GetComponent<Animator> ();
         NeedsToGoAnim = ToiletIcon.GetComponent<Animator> ();
         SleepIcon.enabled = false;
         ToiletIcon.enabled = false;
         Fader.enabled = false;
     }
 
     void Update ()
     {
         ChangeUIBars ();
     }
 
     public void PlayAnim (int AnimHash)
     {
         if (AnimHash == 0) {
         Fader.enabled = true;
             FadeAnim.SetTrigger (FadeHash);
         } else if (AnimHash == 1) {
         SleepIcon.enabled = true;
             SleepyAnim.SetTrigger (SleepyHash);
         } else {
             if (AnimHash == 2) {
             ToiletIcon.enabled = true;
                 NeedsToGoAnim.SetTrigger (NeedsToGoHash);
             }else{
             SleepIcon.enabled = false;
             ToiletIcon.enabled = false;
             Fader.enabled = false;
             }
         } 
         
     }
 
     public void ChangeUIBars ()
     {
         HealthBar.fillAmount = S.health/P.Health;
         HungerBar.fillAmount = S.hunger/P.Hunger;
     }
 
 }

and here is the pic link text

Comment
Add comment · Show 5
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 10chaos1 · Apr 03, 2016 at 07:45 PM 0
Share

ok so after looking again its safe to say that line 39 is not relevant to this as it calls 65 and 66

avatar image Ali-hatem · Apr 03, 2016 at 08:00 PM 0
Share

are you trying to make Survivor & PlayerStats a static scripts if you did that you don't need a reference to call them .

avatar image 10chaos1 Ali-hatem · Apr 03, 2016 at 08:04 PM 0
Share

no i wasn't, thanks for pointing that out, however what would that have to do with NREs ?

avatar image Ali-hatem 10chaos1 · Apr 03, 2016 at 08:45 PM 0
Share

so if they are not a static classes then you need to find the object that they attached to :

   GameObject obj;
   Survivor S;
   PlayerStats P;
   
 void Start(){
 obj = GameObject.Find("the name of object that Survivor class is ittached to");
   if(obj != null)
   S = obj.GetComponent<Survivor>(); 
   //if they are attached to the same object
   P = obj.GetComponent<PlayerStats>(); 
   // if not let me know
   }
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

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

Having a issue with a NullReferenceException 1 Answer

Not sure what this error is telling/asking me to do? 1 Answer

Using TextMeshProUGUI variable in a static function is returning a NullReferenceException 0 Answers

Unity Bug? null reference exception, Only its not null... 0 Answers

Object reference not set message, directly after instantiation 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