- Home /
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");
}
}
}
}
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" ){
}
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.
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 } }
I just tried that out and it worked well. Thanks for the tip!
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
Follow this Question
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