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
0
Question by agito1987 · Oct 03, 2015 at 02:42 PM · c#menubool

Menu made of gameobjects C#

Hallo I was trying to make a menu where my buttons will be 3D object. Right now all the code is supposed to do is cycle trough the 3 menu objects and move them. (visualize selection)

If I put only 2 buttons in the script it works, however with all 3 of them help and options always gets skipped and it seems that the input is not register the right way.

I can’t figure out why it works with 2 buttons but not all 3. Thanks in advance.

 using UnityEngine;
 using System.Collections;
 
 public class Menu : MonoBehaviour {
 
 
     public GameObject menuNewGame;
     public GameObject menuContinue;
     public GameObject menuHelpOptions;
     public float rSpeed;
     public float menuMoveSpeed;
     public bool selectNewGame;
     public bool selectContinue;
     public bool selectHelpOptions;
 
 
     
     // Update is called once per frame
     void Update () 
     {
     
         var wasUpHit = Input.GetKeyDown(KeyCode.UpArrow);
         var wasDownHit = Input.GetKeyDown(KeyCode.DownArrow);
         var wasLeftHit = Input.GetKeyDown(KeyCode.LeftArrow);
         var wasRightHit = Input.GetKeyDown(KeyCode.RightArrow);
 
 
 
         if(selectNewGame == true)
         {
             selectContinue = false;
             selectHelpOptions = false;
 
             //menuNewGame.transform.position = new Vector3 (transform.position.x,transform.position.y,-6.5f);
 
             menuNewGame.transform.position = new Vector3 (-4.78f,0.74f,-6.5f);
             //menuNewGame.transform.Translate(0f,0f,-0.14f);
 
             if(selectNewGame == true && wasDownHit == true)
             {
                 selectContinue = true;
                 //selectHelpOptions =false;
                 //selectNewGame = false;
             }
             if(selectNewGame == true && wasUpHit == true)
             {
                 selectHelpOptions =true;
                 //selectContinue = false;
                 //selectNewGame = false;
             }
 
             
         }
 
         if(selectContinue == true)
         {
             selectNewGame = false;
             selectHelpOptions =false;
 
             //menuContinue.transform.position = new Vector3 (transform.position.x,transform.position.y,-6.5f);
             menuContinue.transform.position = new Vector3 (-4.78f,0.1f,-6.5f);
 
             
             if(selectContinue == true && wasDownHit == true)
             {
                 selectHelpOptions =true;
                 //selectNewGame = false;
                 //selectContinue = false;
             }
 
             if(selectContinue == true && wasUpHit == true)
             {
                 selectNewGame = true;
                 //selectHelpOptions =false;
                 //selectContinue = false;
             }
 
 
         }
 
         if(selectHelpOptions == true)
         {
             selectNewGame = false;
             selectContinue = false;
 
             //menuHelpOptions.transform.position = new Vector3 (transform.position.x,transform.position.y,-6.5f);
             menuHelpOptions.transform.position = new Vector3 (-4.78f,-0.53f,-6.5f);
 
 
             
             if(selectHelpOptions == true && wasUpHit == true)
             {
                 selectContinue = true;
                 //selectNewGame = false;
                 //selectHelpOptions =false;
             }
             if(selectHelpOptions == true && wasDownHit == true)
             {
                 selectNewGame = true;
                 //selectContinue = false;
                 //selectHelpOptions =false;
 
             }
 
         }
 
 
 
 
         if(selectNewGame == false)
         {
             //menuNewGame.transform.position = new Vector3 (transform.position.x,transform.position.y,-6.35f);
             menuNewGame.transform.position = new Vector3 (-4.78f,0.74f,-6.35f);
 
         }
 
         if(selectContinue == false)
         {
             //menuContinue.transform.position = new Vector3 (transform.position.x,transform.position.y,-6.35f);
             menuContinue.transform.position = new Vector3 (-4.78f,0.1f,-6.35f);
 
         }
 
         if(selectHelpOptions == false)
         {
             //menuHelpOptions.transform.position = new Vector3 (transform.position.x,transform.position.y,-6.35f);
             menuHelpOptions.transform.position = new Vector3 (-4.78f,-0.53f,-6.35f);
 
         }
 
 
     }
 }

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

2 Replies

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

Answer by maccabbe · Oct 03, 2015 at 03:39 PM

If you need to learn about programming then please go through some scripting tutorials, i.e..

https://unity3d.com/learn/tutorials/topics/scripting

Suppose when you reach line 79 you have set selectHelpOptions = true due to wasXHit=true. Then you still have to run lines 79-101 which causes selectHelpOptions to be set to not true. To avoid doing so you would normally use if/else if/else instead of 3 consecutive ifs.

However your code could be better written if you used enumerators, switch statements, and by using multiple methods.

Enums

You are using 3 variables, selectNewGame, selectContinue, and selectHelpOptions to represent 1 value: the selected button. Instead you can use an enumerator. Declaring an enumerator is done like so:

 public enum MainMenuButton
 {
         NewGame, Continue, HelpOptions, 
 }

Then you can use the enumerator as a type

 MainMenuButton selected=MainMenu.NewGame;

Switch

Like if/else, switch statements are a control mechanism. They can check a value and take an action depending on what the value is. For instance

 switch(selected)
 {
       case MainMenuButton.NewGame:
             selected=MainMenuButton.Continue;
             break;
       case MainMenuButton.Continue:
             selected=MainMenuButton.HelpOptions;
             break;
       case MainMenuButton.HelpOptions:
             selected=MainMenuButton.NewGame;
             break;
 }

Multiple Methods Your method is long which is a bad sign. Generally each method should do one thing and a long method is typically a sign that the method is doing multiple things. Of course there are long methods that do one thing but this is not one of them.

Your method decides what action to take based on the input, does those actions, then moves the transform. It would be better to make a method that decides what action to take based on the input, a method for each of those actions, and a method to move the transform. For instance:

 using UnityEngine;
 using System.Collections;
 
 public class Menu : MonoBehaviour
 {
     public GameObject menuNewGame;
     public GameObject menuContinue;
     public GameObject menuHelpOptions;
 
 
     private enum MainMenuButton
     {
         NewGame, Continue, HelpOptions,
     }
 
     MainMenuButton selected;
 
     void Start()
     {
         selected = MainMenuButton.NewGame;
         UpdateDisplay();
     }
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             MoveUp();
             UpdateDisplay();
         }
 
         if (Input.GetKeyDown(KeyCode.DownArrow))
         {
             MoveDown();
             UpdateDisplay();
         }
     }
 
     private void MoveDown()
     {
         switch (selected)
         {
             case MainMenuButton.NewGame:
                 selected = MainMenuButton.Continue;
                 break;
             case MainMenuButton.Continue:
                 selected = MainMenuButton.HelpOptions;
                 break;
             case MainMenuButton.HelpOptions:
                 selected = MainMenuButton.NewGame;
                 break;
         }
     }
 
     private void MoveUp()
     {
         switch (selected)
         {
             case MainMenuButton.NewGame:
                 selected = MainMenuButton.HelpOptions;
                 break;
             case MainMenuButton.Continue:
                 selected = MainMenuButton.NewGame;
                 break;
             case MainMenuButton.HelpOptions:
                 selected = MainMenuButton.Continue;
                 break;
         }
     }
 
     private void UpdateDisplay()
     {
         switch (selected)
         {
             case MainMenuButton.NewGame:
                 menuNewGame.transform.position = new Vector3(-4.78f, 0.74f, -6.5f);
                 menuContinue.transform.position = new Vector3(-4.78f, 0.1f, -6.35f);
                 menuHelpOptions.transform.position = new Vector3(-4.78f, -0.53f, -6.35f);
                 break;
             case MainMenuButton.Continue:
                 menuNewGame.transform.position = new Vector3(-4.78f, 0.74f, -6.35f);
                 menuContinue.transform.position = new Vector3(-4.78f, 0.1f, -6.5f);
                 menuHelpOptions.transform.position = new Vector3(-4.78f, -0.53f, -6.35f);
                 break;
             case MainMenuButton.HelpOptions:
                 menuNewGame.transform.position = new Vector3(-4.78f, 0.74f, -6.35f);
                 menuContinue.transform.position = new Vector3(-4.78f, 0.1f, -6.35f);
                 menuHelpOptions.transform.position = new Vector3(-4.78f, -0.53f, -6.5f);
                 break;
         }
     }
 }

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 agito1987 · Oct 03, 2015 at 04:03 PM 0
Share

Hey thank you for the replay this really helped me. And yeah still trying to get a hang of scripting, already went through those base tutorials. Biggest problem for me is still not always knowing what the best way is to approach different problems.

Now I know where to look and approach this problem the right way, thank you!

avatar image
1

Answer by Lairinus · Oct 03, 2015 at 03:51 PM

The Problem

In short, your code is falling through because you aren't using if-else. You are setting a variable, and then using that variable with the result you just set. And this happens multiple times.

  • If you 'selectNewGame; and then hit down, you will set 'selectContinue.'

    Then you immediately fall through to 'selectContinue'.

    Then from there, since downWasHit is true, and selectcontinue is true, you automatically select Help Options.

The fix:

Use If-Else instead of just If. If should not be used if you are comparing the same variable, and the variable changes values often, unless that is intended functionality.

If this is still not working for you then let us know.

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 agito1987 · Oct 03, 2015 at 04:05 PM 0
Share

Yeah first it was “if else”. Removed the else to try fix it myself, but forgot to put it back in. but even with “if else” I had the same problem.

But thank you for pointing it out!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Bool wont change... #C 1 Answer

Help with input code please (c#) 1 Answer

Unity 2D Title Screen W/ Keyboard Input ONLY! 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