• 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 straydogstrut · Apr 10, 2010 at 05:05 PM · gameobjectvariablewarnings

Correct way to determine if a variable has a value

I'm running some code on a gameObject assigned through a variable in the Inspector. However in some cases there isn't a gameObject to assign, so I am attempting to double check first whether a gameObject has been assigned to the variable.

I have tried the different code snippets below, but I still get a warning: "UnassignedReferenceException: The variable myWorldObj of 'visualControl' has not been assigned. This happened in the game object..."

if(myWorldObj != null){ // do stuff with this object here }

if(myWorldObj){ // do stuff with this object here }

if(!myWorldObj == null){ // do stuff with this object here }

I've successfully checked if components like scripts exist using GetComponent, but I don't know how to do it for individual values like this. My game runs fine, but seeing the warning at the bottom of the window upsets me.

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

2 Replies

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

Answer by Eric5h5 · Apr 10, 2010 at 05:29 PM

Your first two methods are correct and won't generate any warnings or errors. The third method will compile, but will never execute the code between the braces no matter whether myWorldObj is assigned or not, and if you're using C# you should get an unreachable code warning. Any errors you're getting while using the first two methods are the result of something elsewhere.

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 straydogstrut · Apr 10, 2010 at 05:43 PM 0
Share

Thank you both, I thought one of the first two was right since I'm sure i've used it before. Yes, I've got in the habit of clearing the log window regularly. I'm still getting the error so it must be something elsewhere. Time to go treasure hunting...

avatar image straydogstrut · Apr 10, 2010 at 05:47 PM 0
Share

Oh! Well that didn't take long. I was turning the gameObject off in my Start() function. I've added a check in there too and all is right with the world now=)

avatar image
1

Answer by Ashkan_gc · Apr 10, 2010 at 05:40 PM

the warning that you see is not for your current state of your game. just hit the clear button of log window. :) the message is for your previews code. use first or second method and you are good to go.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Keeping a destroyed game object afterwards 0 Answers

How to deactivate something when holding down a key? 1 Answer

How do I detect if a game object exist? 4 Answers

How can I change a variable in a different game object's script? 2 Answers

get the gameobject's 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