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 MrFieryIce · Dec 02, 2014 at 05:22 AM · errornullreferenceexceptionreferencenoob

NullReferenceException

Hi. I am completely lost on this, and I've been trying to find a previously answered question, but none of them really fits my application. I am not entirely sure how I'm getting this error. I am trying to make my script that is in a separate GameObject control a GUIText.alt text

I'm trying to connect the empty Game Manager GameObject to the "InfoText" TextGUI that is highlighted.

Please help me clean up this script. (I prefer UnityScript/JavaScript thank you.)

 private var InfoText : GUIText;
 
 function Awake(){
     var InfoText = GameObject.Find("/Building Mode GUIs").guiText;
     InfoText = gameObject.GetComponent("Text").guiText;
 }
 
 function Update(){
     InfoText.text = "Total Cost: " + PriceAdding;

Error Message :

 NullReferenceException: Object reference not set to an instance of an object
 Game Manager.Awake () (at Assets/Game Manager.js:16)

screenshot_6.png (124.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

3 Replies

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

Answer by PAHeartBeat · Dec 03, 2014 at 04:58 AM

Hi

In line 4 you alread take GUI Componanet in "InfoText" variable so yo don't need line 5 just remove it but your line 4 is also wrong as per your screen shot

  1. In screen shot you have "GUI Text" component on GameObject "InfoText"

  2. "InfoText" GameObject is Child of "Building Mode GUIs"

so your code should like this

     var InfoText = GameObject.Find("Building Mode GUIs/InfoText");
     var myGuiText : GUIText;
     if(InfoText) {
         myGuiText = InfoText.GetComponent<GUIText>();
     }
     if(myGuiText) {
         myGuiText.text = "value";
     }


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 MrFieryIce · Dec 03, 2014 at 05:35 AM 0
Share

Thanks man!

avatar image PAHeartBeat · Dec 03, 2014 at 05:52 AM 0
Share

always Welcome bro..

avatar image
1

Answer by robertbu · Dec 02, 2014 at 05:24 AM

GUIText is not of type Text. Avoid using the string version of GetComponent() Assuming your GameObject.Find() is succeeding and the game object has a GUIText component, then delete line 5. Line 4 should work to get access to the GUIText component.

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 JA_555 · Dec 03, 2014 at 09:23 AM

Perhaps you could try:

     private var InfoText : Text;

     function Awake(){
     InfoText = GameObject.Find("/Building Mode GUIs").text
     }

     function Update(){
     InfoText.text = "Total Cost: " + PriceAdding;
 

I think you just confused your GUIText and text.

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 Lunatix · Dec 03, 2014 at 10:33 AM 0
Share

This is very unstable as you do not check for null values - if the Find method returns null, a null pointer exception will be thrown on Awake and also every update call.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to call a function from another script - getting error "Object reference not set to an instance of an object" 5 Answers

NullReferenceException Error 6 Answers

SetActive is giving me a lot of issues 2 Answers

NullReferenceException: Object reference not set to an instance of an object 0 Answers

Trying to create a new script in javascript error 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