- Home /
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);
}
}
}
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;
}
}
}
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!
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.
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
Follow this Question
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