If statement not working correctly C#
So I have a script : [HeaderAttribute("Things that are required to fall out")] public Transform WoodObject; public Transform WoodObject1; public Transform WoodObject2; public Transform WoodObject3; public Transform WoodObject4; public Transform WoodObject5; public Transform WoodObject6; public Transform WoodObject7; public Transform WoodObject8; public Transform WoodObject9; public Transform WoodObject10; public Transform WoodObject11; public Transform WoodObject12; public Transform WoodObject13;
[HeaderAttribute("The CantGoPast")]
public GameObject ObjectCantGoBy;
// the things above declares all the variables
void Update ()
{
// this is supposed to make it transition to a scene called MainMenu when the WoodObject goes past the y axis of the variable "ObjectCantGoBy"
if (WoodObject.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
} else if (WoodObject1.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
} else if (WoodObject2.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
} else if (WoodObject3.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
} else if (WoodObject4.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
} else if (WoodObject5.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
} else if (WoodObject6.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
} else if (WoodObject7.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
} else if (WoodObject8.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
} else if (WoodObject9.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
} else if (WoodObject10.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
} else if (WoodObject11.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
} else if (WoodObject12.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
} else if (WoodObject13.transform.position.y <= ObjectCantGoBy.transform.position.y) {
EditorSceneManager.LoadScene ("MainMenu");
return;
}
else
{
Debug.Log("The Object is at" + WoodObject.position.y + " is greater than ObjectThatCantGoBy, which is at" +ObjectCantGoBy.transform.position.y );
return;
}
}
}
You haven't stated what isn't working. Also, please put all code in correct formatting.
If statements work correctly 100% of the time, no exceptions. If it doesn't do what you want, you programmed it wrong. Also, you really need to use arrays, not a whole bunch of separate variables.
The whole code feeels so wrong, actually it shouts "kill me". A script on your wood objects should work better for you.
It looks like you're just starting to learn program$$anonymous$$g. This is the standard way-too-long example of why we want to learn arrays.
Answer by bubzy · Oct 30, 2016 at 08:44 AM
"You Keep Using That elseif
, I Do Not Think It Means What You Think It Means" - Inigo Montoya, 1987
I'm always a bit dumb with elseif
but i don't think the way you used it works. try using if
instead. also how would you know which object went past? (if this is important), you may want to return a number value inside the 'if' so that you can write something like
Debug.Log("the object " + returnedWoodValue + "went past.........."); //or whatever :)
That's a standard cascading if. What it does is odd, but the form is right out of a textbook.
Older languages had a specific elseif command - all one word. But new languages like C# got rid of it. We realized it was pointless. You just write an else, followed by an if.
ha :) it just looks weird, but i think i get it. maybe i should use this more......
Well, the OPs usage is very odd and doesn't show the point of doing this. The else's didn't need to be there in this case. All my examples of useful cascading ifs are in the second IF chapter in taxexforcatses.com. But I imagine you can search for "cascading if" and find some decent ones.
A very simple way of thinking of multiple if's vs a elseif, is:
int x = 1;
if (x == 1)
DoSomething();
if (x == 2)
DoSomethingElse();
Per that example, let's say: DoSomething() changes x to 2. So, not only will the first if be triggered, the second one will as well.
Now, if you did:
int x = 1;
if (x == 1)
DoSomething();
else if (x == 2)
DoSomethingElse();
Where once again, DoSomething() changed x to 2, however, the second if will NOT be triggered.
I hope that helps.
Your answer
Follow this Question
Related Questions
Add changeable conditions in the editor, similar to unityEvent? 0 Answers
How to return back to false? 1 Answer
If Statement not working properly - Comparing game objects 0 Answers
How to detect collisions in the LateUpdate ? 0 Answers
how can i check if two numbers are close to each other? 1 Answer