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 /
  • Help Room /
avatar image
-1
Question by JeffHardddyyy · Oct 30, 2016 at 07:51 AM · c#if

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;
         }
     }
 }


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 Namey5 · Oct 30, 2016 at 08:16 AM -1
Share

You haven't stated what isn't working. Also, please put all code in correct formatting.

avatar image Eric5h5 · Oct 30, 2016 at 09:23 AM 0
Share

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.

avatar image ananasblau · Oct 30, 2016 at 12:32 PM 0
Share

The whole code feeels so wrong, actually it shouts "kill me". A script on your wood objects should work better for you.

avatar image Owen-Reynolds · Oct 30, 2016 at 02:53 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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 :)
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 Owen-Reynolds · Oct 30, 2016 at 02:36 PM 0
Share

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.

avatar image bubzy Owen-Reynolds · Oct 30, 2016 at 02:38 PM 0
Share

ha :) it just looks weird, but i think i get it. maybe i should use this more......

avatar image Owen-Reynolds Owen-Reynolds · Oct 30, 2016 at 02:58 PM 0
Share

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.

avatar image Cynikal ♦ · Oct 30, 2016 at 06:15 PM 0
Share

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

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

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


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