Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Anne-Pier · Oct 23, 2017 at 09:37 AM · operator

Question about ?: (if/else) operators

I learned to use these operators only a few days ago so I'm still trying to figure out how to them exactly. Here's a problem I have though:

When I use it like this it works:

 intName = condition
 ?  1 
 :  2

But when I try this it doesn't work

 condition 
 ? intName 1 
 : intName 2

While it doesn't matter in this situation cause I can just use the first one, it does matter in other situations. I'm currently trying to do this:

 GameObject == null ? StartCoroutine(Name()) : doSomething();


The error I'm getting is the following :

Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement.


Seen this error a lot but in this certain situation I don't know how to fix it. Thanks in advance :), also if anyone wants to tell me how to show my code better instead of typing it out here feel free too cause I'm kinda new to asking questions so I have no idea how everyone does that.

Comment
Add comment · Show 4
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 Dragate · Oct 23, 2017 at 10:11 AM 0
Share

When you post something there is a bar above that gives several options (like the Bold you used). In the same bar there is an icon with some binary. Select your code you've written/pasted and click that icon.

avatar image Anne-Pier Dragate · Oct 24, 2017 at 09:46 AM 0
Share

Thanks ;p, Idk how it changed in my post though. I didn't do anything :O

avatar image Hellium · Oct 23, 2017 at 02:37 PM 0
Share

Here is a workaround : https://stackoverflow.com/questions/5490095/method-call-using-ternary-operator

avatar image Anne-Pier Hellium · Oct 24, 2017 at 09:46 AM 0
Share

I'll check it out thanks.

2 Replies

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

Answer by MNNoxMortem · Oct 23, 2017 at 10:21 AM

?: is not the very same as if/else, it is a conditional operator (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator). You can use it to assign the conditional values to some variable, but not as a if/else replacement when you do not have an assignment.

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 NoseKills · Oct 23, 2017 at 02:28 PM 1
Share

Yup. For example

 true ? 5 : 10;

is an expression that has a value just like a variable has a value. This particular expression always has the value 5.

Writing that single line of code is not a valid statement just like it wouldn't be if you just wrote its real value

 void Start() { 
      5;
 } // Gives an error

Assigning a value however is a valid statement so you can write

 int variable = 5;
 // Or
 int variable = true ? 5 : 10;



avatar image MNNoxMortem · Oct 23, 2017 at 04:21 PM 1
Share

Damn, i misclicked. That should be a comment ins$$anonymous$$d of an answer. See Nose$$anonymous$$ills comment above. I was too lazy to explain it in depth and he did so.

avatar image
1

Answer by AlbertoFdzM · Oct 23, 2017 at 02:39 PM

That way to use if/else is called "ternary operator". It's made to be used in expressions that have to return some value. In other words, whenever you use a ternary operator you are telling to the program that you are going to return something from that expression. So, for example, the line true == true ? 1 : 2 is going to return 1 when executed. C# doesn't allow expresions that returns something if you'r not assigning the returned value to any variable. if you do this int myVal = true == true ? 1 : 2 the expression will be executed correctly and myVal will have a value of 1. That's the difference between the regular if/else declaration and the ternary operator.

Comment
Add comment · Show 1 · 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 Anne-Pier · Oct 24, 2017 at 09:35 AM 1
Share

Thank you.

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

74 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 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 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

How can I overload operators in Unity Javascript 1 Answer

< cheaper than <= ? 2 Answers

Operator '+' cannot be used with a left hand side of type 'Object' and a right hand side of type 'float' 2 Answers

Trouble with understanding code that uses "==" operator. 2 Answers

How do use the % operator? 2 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