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 ina · Nov 07, 2012 at 08:12 AM · javascript

Does the || operator always work in UnityJS retrieval?

var go:GameObject;
var goleaveblank:GameObject;
private var go_assignme:GameObject;

Suppose you have three exposed game objects. Only go is assigned - say - a cube in Hierarchy.

go_assignme = goleaveblank || go; // a || default value

assuming goleaveblank=null, will the default value always be assigned, as in: `go_assignme = go;` ?

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 deltamish · Nov 07, 2012 at 08:36 AM 0
Share

I dont think so why do you want to use || why not just assign one of them

avatar image ina · Nov 07, 2012 at 08:47 AM 0
Share

to avoid script errors when referencing an unexpected null object, it is safer to define a default value. the above syntax works currently, but i am wondering if it works always

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Nov 07, 2012 at 11:07 AM

This should not work. || is a logical operator and should only return boolean. C# would return an error. I would guess the compiler is converting go_assignement to a boolean so that you do not see any error but with C# it shows right on.

You might as well go with the ternary operator:

 GameObject obj = goleaveblank ? goleaveblank:go;

If goleaveblank is null then go is return.

Note this is just a fancy way for:

 GameObject obj;
 
 if(goleaveblank)obj = goleaveblank;
 else obj = go;

EDIT: Based on Izitmee comment I just add his version:

  GameObject obj = goleaveblank??go;

All credits to him on that one.

Comment
Add comment · Show 5 · 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 Demigiant · Nov 07, 2012 at 11:17 AM 1
Share

I don't know if UnityScript supports this (I'm just sure Unity C# does), but since we're talking about assigning an object if it's not null, and another object if the previous one was null, this would be even more fancy ;)

 GameObject obj = goleaveblank ?? go;
avatar image fafase · Nov 07, 2012 at 11:20 AM 0
Share

Oh oh!!! Never seen that one before!

avatar image Demigiant · Nov 07, 2012 at 11:24 AM 0
Share

It's a very nice thingie: it returns the left operand if not null, and the right operand if the left one was found null http://msdn.microsoft.com/en-us/library/ms173224.aspx

avatar image ina · Nov 07, 2012 at 09:29 PM 0
Share

@fafase actually if you try .js, you will find that || works

avatar image fafase · Nov 08, 2012 at 06:12 AM 0
Share

But does it do what is expected? Or does it convert go_assigne into a boolean? I just think this is why some people say C# is safer than UnityScript.

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

12 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

Related Questions

assigned function declaration in unity javascript for different types 1 Answer

Referencing subclass in Unity JS 2 Answers

Closure does not work in Unity JS functions? 1 Answer

UnityEngine.Rect' does not have a visible constructor that matches the.... 1 Answer

Coroutine gets skipped in javascript/unityscript 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