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 TheXWolf · Feb 15, 2016 at 07:49 PM · c#editornull reference exception

Null Reference but Referenced In Editor

I'm working on a C# script that counts my inventory, every item picked up adds to a basic int count. I've done tons of Debug.Log() to check that the item is properly being added and the counts are correct, and all are. However, when I go to set the Text.text of my UI Text object I get a null reference exception. I have tried GetComponent, GameObject.Find (which I hate but had to try) and the normal method on which the Text field is assigned is via a private but serialzed value in the script, and referenced in the editor.

I have done null checks in awake to make sure nothing is changing the values by accident during start, and from the instant runtime is initiated I'm immediately getting a null reference.

 [SerializeField]
 private GameObject totalSlider;
 [SerializeField]
 private GameObject totalText;
 
 totalText.GetComponent<Text>().text = string.Format ("{0} / {1}", Count, MaxCap);
 
 totalSlider.GetComponent<Slider>().value = Count;

I have also referenced them directly as and in Editor to expose .text and .value directly, but same results. Have also searched for them in scene by name, without return, and also deleted them and added them again both in and out of the prefab. Checked to make sure it's the objects themselves and not the variables that are nulled, but they are returning values nicely both in and out of debug. Wanted to check to see if there's an obvious mistake I'm making before I email unity directly.

Comment
Add comment · Show 1
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 Glurth · Feb 17, 2016 at 03:59 PM 0
Share

I'm not clear on whats co$$anonymous$$g back null: is it totalText,
totalText.GetComponent() or totalText.GetComponent().text

If the issue is with totalText: can you confirm this GameObject exists at scene start? If the totalText object is created dynamically, after scene start: totalText's value at scene start will always be invalid, until set to the newly created object. Obviously same thing goes for the Text Component attached to the totalText gameobject.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kloudon716 · Feb 15, 2016 at 08:26 PM

I think you need to initialize totalText to the component and then use it. See below. Same for the slider. and if the component is not on the game object this script is on then you will need to find the object first.

 totalText = GetComponent<Text>();
 totalText.text = string.Format //etc.
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 TheXWolf · Feb 16, 2016 at 04:59 PM 0
Share

They are both referenced in the editor itself. After hitting play I can still see them attached, but continue getting a null reference.

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

104 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

Related Questions

Unity 2020.1 changing Script triggers complete reimport? 0 Answers

How to save a two-dimensional array, as part of the variable inspector? 0 Answers

Unity editor toggle Sceneviews skybox and effects 0 Answers

Setting instance parent delays scene view update 0 Answers

Array index out of range. But it doesn't appear to be. 2 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