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 Stealthygolem · Dec 05, 2014 at 01:18 PM · c#gameobjectnullreferenceexceptionnulldialogue

How do I nullreference this correctly?

Hello! I am playing around with some dialogue-scripting from the ground up. And I am having a bit of an issue with nullreferencing something that isn't active.

So this is the code that will check for a gameobject:

         GameObject textInBubble = GameObject.Find ("TextInBubble");
         TextMesh textMeshThing = (TextMesh)textInBubble.GetComponent(typeof(TextMesh));

The thing is, that what this code wants to find is on standard set to be inactive. It only becomes active when a dialogue is occurring, and on and off relative to the conversation in question.

I know I could force this code to work if I paste it in everytime I actually state that this gameObject is active, which would look something like this:

             if(i == 0){
                 speechBubbleActive = true;
                 GameObject textInBubble = GameObject.Find ("TextInBubble");
                 TextMesh textMeshThing = (TextMesh)textInBubble.GetComponent(typeof(TextMesh));
                 textMeshThing.text = "Hello Unity Community!";
             }
             else{speechBubbleActive = false;}

Obviously, this is unwanted behavior, it would create a mass amount of duplicate entries in my code..

There has to be a way to do checks every frame. With a correct nullreference when the gameobject is not active. Any help or direction is appreciated. I tried finding it out by my own but I have had no luck so far. Thanks!

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
1
Best Answer

Answer by gjf · Dec 05, 2014 at 01:25 PM

if you're setting flags when it's in/active then checking that before attempting to check the component would work, but you should null check after every component get/find/etc.

 if (speechBubbleActive)
 {
     GameObject textInBubble = GameObject.Find ("TextInBubble");
     if (textInBubble != null)
     {
         TextMesh textMeshThing = textInBubble.GetComponent<TextMesh>();
         if (textMeshThing != null)
         {
             textMeshThing.text = "Hello Unity Community!";
         }
     }
 }


ideally, you'd get these references in Start()/Awake() to save having to do it every time, then you just need the null check...

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 Stealthygolem · Dec 05, 2014 at 01:29 PM 0
Share

I'm trying out this thing in start method right now, I just remembered that it might work, haha. check with you later

avatar image Stealthygolem · Dec 05, 2014 at 01:37 PM 0
Share

Just remembered I could do this in my Start() method, and it worked fine when doing my checks:

     void Start() {
         speechBubble = GameObject.Find ("SpeechBubbleJohn");
         textInBubble = GameObject.Find ("TextInBubble");
         text$$anonymous$$eshThing = (Text$$anonymous$$esh)textInBubble.GetComponent(typeof(Text$$anonymous$$esh));
     }

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

26 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

Related Questions

node null reference 1 Answer

Setting Prefab with Code Returns NULL 2 Answers

Simple Looping drag and drop game 1 Answer

Instantiate gameObject returns null? 1 Answer

Checking if an object is null, results in NRE 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