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 question was closed May 07, 2018 at 08:19 AM by Myth for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Myth · Feb 08, 2011 at 04:49 AM · gameobjectvariablenullif

if syntax for blank variable.

Hi all,

I need to know the syntax for testing if a transform, gameobject, and camera variables have anything in them.

eg.

if (var has contence)
{
   do stuff
}

Any help would be appreciated.

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

  • Sort: 
avatar image
4
Best Answer

Answer by Eric5h5 · Feb 08, 2011 at 05:28 AM

Test whether it's null or not:

if (someVar != null) {stuff}
// or
if (someVar) {stuff}
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
1

Answer by Simple · Feb 08, 2011 at 05:05 AM

better to use print() and console or logs, so you will get more info:

var x = ...//gameobject, transform, etc print(x.anyproperty);

like

print(x.name); //for gameObject print(x.position); //for Transform etc

if its empty it will return Null or wrong reference object.

You just need to know properties of different objects.

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 Myth · Feb 08, 2011 at 05:20 AM 1
Share

sorry, I didn't mean in a debug context, I need to test if the object is there so I can do stuff to it if it is.

avatar image Myth · Feb 08, 2011 at 05:22 AM 1
Share

to avoid this error:

UnassignedReferenceException: The variable ship_cam of Posiition_Controll' has not been assigned.

avatar image
0

Answer by Mantiz-GS · Aug 08, 2013 at 03:01 PM

Hi.. maybe you are looking for something like this...

Using variable texture as a public variable, but not asigned in the inspector public Texture2D textura; if (GUI.Button (new Rect (100,100,100,100), texture)) { try { Debug.Log(texture.name); } catch { Debug.Log("There is no texture"); } }

This code get the unassigned error, but I don't know if is the best way to implement a try catch code because use if (texture==null) do not work and shows the same error. Hope someone have a better answer for this.

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

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

empty component var 1 Answer

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

How to change variables on a new gameobject's script 1 Answer

Calling an if statement once even when it is calling every frame? 3 Answers

Store previous gameObject that raycast had hit. (Or store any previous value in Update for that matter) 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