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 smnerat · Dec 02, 2012 at 10:27 PM · raycastiflinecast

Problems using an or (II) inside of an if statement.

Hey guys,

When I run my script with a single variable such as (== "Capsule1I") inside of my if statement everything works the way I intend. When I attempt to use (== "Capsule1I" || "Capsule8I"), I always get true out, no matter what is detected by the linecast. Is there a problem with my syntax or method? There are a total of 4 possible capsules that I would like to use (== "Capsule1I" || "Capsule8I" || "Capsule16I" || "Capsule24I"). Is this possible or do I need to try something else?

 var hit : RaycastHit;
 var line : RaycastHit;
 var cp : boolean = false;
 
 
 function Update(){
 
     if(Input.GetMouseButtonDown(0) && collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, Mathf.Infinity)){
 
         if(Physics.Linecast(Vector3(0,7.25,-5), Vector3(0,7.25,5), line)){
             if(line.collider.gameObject.name == "Capsule1M"){
                 cp = true;
                 Debug.Log("check position true");
             }            
             else {
                 cp = false;
                 Debug.Log("check position false");
             }        
         }                                                                                                    
     }    
 }
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

1 Reply

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

Answer by Caiuse · Dec 02, 2012 at 10:38 PM

I think the syntax of your IF statement may be wrong, try this:

 if( line.collider.gameObject.name == "Capsule1M" || line.collider.gameObject.name == "Capsule2M" ){
      //other stuff
 }

each "OR" ( || ) operator needs full syntax.

if you want it to be a bit easier to read then I would store the gameObjects name in a instance variable:

 var name : String = line.collider.gameObject.name;
 
 if( name == "Capsule1M" || name == "Capsule2M" ){
 
 }


Comment
Add comment · Show 4 · 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 smnerat · Dec 02, 2012 at 10:50 PM 0
Share

Thank you very much. Firstly, I should know better, I've made this mistake before. I appreciate the answer nonetheless.

Secondly (a tangent really), referring to the ease of reading... I think I am going to type the whole thing out despite having to re use the above code for 16 positions. It seems like a lot but once its done, I can apply this to all levels of my game (this is the win scenario code). I've thought about it and really haven't been able to come up with a sleek way of doing it.

avatar image Caiuse · Dec 02, 2012 at 11:06 PM 0
Share

There are various approaches to this, if your just checking a set of variables to see if any return true then consider wrapping this function:

var names = ["Capsule1","Capsule2",'Capsule3"];

function checkNames( arrayToCheck : string[], comparison : string ) : boolean { for(var i = 0; i < arrayToCheck.length;i++){ if(arrayToCheck[i] == comparison) return true; } }

function Start(){ if(checkName(names,line.collider.gameObject.name)){ //blah } }

avatar image smnerat · Dec 03, 2012 at 01:06 AM 0
Share

I just tried that out and it worked well. Thanks for the tip!

avatar image Caiuse · Dec 03, 2012 at 09:41 AM 0
Share

No problem, glad to help!

( If thats answered your question, please could you click the little tick symbol on the post to mark this as answered. )

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Using a raycast to check for objects between two sprites? 1 Answer

Have to press Fire1 button twice to run if statement? 1 Answer

Using different paricle emitters depending on the tag of the object raycast hits? 1 Answer

Help with key code!! 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