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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Danor · Feb 07, 2015 at 08:04 PM · c#androidnullreferenceexception

How to check for null

For a dynamic menu with 4 (1,2,3,D) levels in (x amount of) missions. I have a Typer script on a text that activates when the player clicks a button but when he clicks the next button: the next Typer writes the text over the other one because both Texts are (and need to be) in the same place.

there are other solutions like using public gameobjects + SetActive but i will have a lot of buttons so this would be inconvenient so instead:

i would make a function that disables all found texts for that mission before enabling the new one but TextLevel3 will sometimes simply not exist and return null(referenceExeption) so i want to check for this and break if its null or continue if it's safe to. I'm just not sure how to do this with combined with the Gameobject.Find and GetComponent

     void DisableTexts()
     {
     text = GameObject.Find("TextLevel1").GetComponent<Typer>().enabled = false;
     text = GameObject.Find("TextLevel2").GetComponent<Typer>().enabled = false;
     text = GameObject.Find("TextLevelD").GetComponent<Typer>().enabled = false;

     //This is what i need: 
       Check if (TextLevel3 != null){go ahead} or if (TextLevel3 = null){break;(?)}

     text = GameObject.Find("TextLevel3").GetComponent<Typer>().enabled = false;
     }

     public void CheckLevel (string selectedLevel)
 {
     DisableTexts();
 GameObject.Find("TextLevel" + selectedLevel).GetComponent<Typer>().enabled = true;
 CurrentMission = "Level" + missionSelected + "." + selectedLevel;    
 }
         
 


Comment
Add comment · Show 2
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 Mmmpies · Feb 07, 2015 at 08:18 PM 0
Share

Why not just hold the text in an array/list, leave the text box blank until needed and the load the required text when needed?

avatar image Bonfire-Boy · Feb 07, 2015 at 08:55 PM 0
Share
 text = GameObject.Find("TextLevel1").GetComponent().enabled = false;

So text is a boolean? You don't use it between the 1st and 4th times that you set it, seems a bit odd to me.

Anyway it looks to me like maybe you want something like

  GameObject g = GameObject.Find("TextLevel3");
  if (g != null)
  {
     Typer t = g.GetComponent< Typer >();
     if (t !=null)
     {
         t.enabled = false; // or whatever else you want to do with it
     }
  }

That'll check not only that the object with that name exists but also that it has a Typer component. It can be that one or other of those tests isn't necessary.

You might also want else clauses that warn you when those things are null.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DiegoSLTS · Feb 07, 2015 at 09:10 PM

 GameObject text3 = GameObject.Find("TextLevel3");
 if (text3 != null) {
     text = text3.GetComponent<Typer>().enabled = false;
 }
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
Wiki

Answer by nirharpaz · Feb 07, 2015 at 08:40 PM

if(x!=null && whatever the condition you want) { do something to x; }

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

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

23 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

Related Questions

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unexplainable NullReferenceException 2 Answers

How to use Joystick from Standard Assets? 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