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 /
avatar image
1
Question by Topthink · Mar 06, 2019 at 12:12 AM · uitext

Why Null Reference Exception here?

I've looked at quite a few script examples here in "Answers" and it looks like I'm doing things correctly but I get a "Null Reference Exception" on line 26.

Background. When I run my program, the "Start" here will print the correctly so I have the UI correctly added to the public Text object. When I run my program, I access this from another script and the function ShowDataCity() is properly accessed (?) because the Debug Log correctly prints "City Data !!!". BUT, then I get an error on the very next line. It seems as though since it works fine in the Start function it should also work in the ShowDataCity() function. Hmmmm.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class DisplayUIInfo : MonoBehaviour
 {
     public Text testInfoData;
 
     private string tempText;
 
     private void Start()
     {
         testInfoData.text = "Start !!!";
     }
 
     public void ShowDataSingleUnit()
     {
         Debug.Log("Single Unit Data !!!");
     }
 
     public void ShowDataCity()
     {
         string stuff = "City Data !!!";
         Debug.Log(stuff);
         testInfoData.text = stuff;  ///<<<< Null Reference Exception
     }
 
     public void ShowDataRawMats()
     {
         Debug.Log("Raw Material Data !!!");
     }
 }

Comment
Add comment · Show 4
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 Topthink · Mar 06, 2019 at 12:21 AM 0
Share

Line 14, where it displays "Start !!!" works fine. But line 26 gets the error. The program runs fine when I start it but when I click on one of my cities I get the error. Even though I get here from another Script, it seems as though it should work since I'm not passing any data from the other script to here.

avatar image Bunny83 · Mar 06, 2019 at 02:27 AM 0
Share

What have you done to debug your problem? Have you actually tried checking "testInfoData" for a null value inside "ShowDataCity"? $$anonymous$$aybe you set it to null from somewhere else? If not we just need more information. For example the exact stacktrace. Is the exception thrown by that line or from inside the text property setter? Are you sure you don't call that method from another thread?

avatar image Topthink Bunny83 · Mar 06, 2019 at 04:08 PM 0
Share

Thanks. I'm still working on this and I appreciate your suggestions and will continue to exa$$anonymous$$e the problem. Funny thing, if I call "ShowDataCity" from the start function of the same script, it will properly display the info without any error. I get the NullRefExeption error only when I call "ShowDataCity" from another script. Oddly, I get the error even when I'm not passing any information from the other script. Again, I'm still looking at this.

avatar image mchts · Mar 06, 2019 at 07:15 AM 0
Share

Clearly testInfoData is being destroyed or sth. at some point. You better track its activity in update

3 Replies

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

Answer by Topthink · Mar 07, 2019 at 08:43 PM

I figured out a "Work Around" for this problem. I've been trying to call a function on one script from another script but I kept getting a NullReferenceException and I couldn't understand why.

 ...declared on ScriptB...
 public ScriptA callFunctionFromA;
 ...called from ScriptB...
 callFunctionFromA FunctionName();

The above had always worked for me in the past but in this case I was getting the NullReferenceException and I'm not sure what I was doing to cause it. In any event I changed how I accessed the other script and it now works as designed.

 ...declared on ScriptB…
 private ScriptA _scriptA;
 ...added to "Start" in ScriptB…
 _scriptA = FindObjectOfType<ScriptA>();
 ...called from ScriptB…
 _scriptA.FunctionName();

Both are fairly standard methods of accessing another script but I normally use the first because it apparently uses less time to work. I've never had a problem with the first method until this last program and again, I'm not sure what I was doing that caused the problem this time around. But using the second method works fine here and I will go ahead and use it for now and try to figure out the specific problem as I gain more knowledge about debugging, etc.

Comment
Add comment · 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
0

Answer by CCnockaert · Mar 06, 2019 at 04:58 PM

Something is destroying the testInfoData Text Object OR, something is breaking the reference to it.

You should replace this line :

 public Text testInfoData;

--> [SerializeField] Text testInfoData;

It will prevent other scripts to break the reference. https://docs.unity3d.com/ScriptReference/SerializeField.html

Comment
Add comment · Show 2 · 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 Topthink · Mar 06, 2019 at 11:22 PM 0
Share

Thank you, I'll try what you are suggesting. I appreciate that you took the time to help me.

avatar image Topthink · Mar 07, 2019 at 06:12 PM 0
Share

This did not work for my situation although, as you point out, it may well work for other similar situations. Thanks for your input, however.

avatar image
0

Answer by NikLaws · Mar 07, 2019 at 08:24 AM

Had the same issue. Fixed it by checking if it's not null.

 if(testInfoData != null) testInfoData.txt = stuff;

it was answered in another thread about NullReferenceException.

Comment
Add comment · Show 2 · 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 Topthink · Mar 07, 2019 at 06:15 PM 0
Share

Thank you. I will try that immediately and see how that impacts my program. I'm still working on debugging this matter as I get the time but I'm not an expert at debugging and it takes me time. I did look through many threads about this matter and didn't see what you indicate although I fully believe what you say and I really appreciate your assistance.

avatar image Topthink · Mar 07, 2019 at 06:33 PM 0
Share

The code you suggest here does indeed prevent the error but the underlying problem still remains which is that I need the info to display. I need to either figure out what is causing the info to be lost or destroyed (I'm still working on this) or I need to figure out an alternate method to get the data to display (I'm working on this as well). I'm not an expert debugger so it's taking me some time to work through all this.

Again, thanks for your help.

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

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

4.6 UI Text rect does not expand automatically 2 Answers

uGUI letter spacing and kerning 1 Answer

Missing canvas elements on Build & Run. 2 Answers

How to change color of animated Text component 2 Answers

Inputfield text to String variable 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