Search Unity

Working with buttons, need help!

Discussion in 'Game Design' started by heff2013, Nov 7, 2019.

  1. heff2013

    heff2013

    Joined:
    Nov 1, 2019
    Posts:
    3
    Hi, my friends and I (Highschoolers taking college courses in trade school) are building our first game in unity and we are going to a SkillsUSA competition for it. We have only been working with C# for one year and having some help would be amazing. We have buttons for a start, options, credits, and exit menu (title screen). Our game is a paper Mario themed game, so lot of the art is cardboard, sticker, and paper. The title screen is a animation of a doodled curtain, both sides swing their designated way (left and right) when the start button is clicked. Except, when you click anywhere on the title screen it starts. How do I fix this?
     
  2. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
    It depends on how your curtain swing animation is triggered. Is it a button click, a mouse down check in an update function? Which GameObject does the code live in?
    We might need to see some code and/or screenshots to help you with this one.
     
  3. heff2013

    heff2013

    Joined:
    Nov 1, 2019
    Posts:
    3
    We fixed that after some work and have most of our menu buttons working. The only issue we have currently have is when we go into the options menu and adjust the music (on/off) button we can click it on or off but when we exit the options menu, the button design (on/off) clone stays on the menu scenes with the start, credit, options or exit and it becomes like an annoying overlay. I'm not sure how to destroy it. Here's our code for buttons
    :

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;
    public class Buttons : MonoBehaviour
    {
    [SerializeField]
    Animator anim;
    [SerializeField]
    Animator animation;

    [SerializeField]
    private int buttonID;

    public GameObject optionMenu;
    public GameObject startMenu;
    public GameObject headCredits;


    private bool musicOn;
    private bool plusVolume;
    private bool minusVolume;

    // Start is called before the first frame update
    void Start()
    {
    musicOn = true;
    musicOn = !musicOn;
    }

    // Update is called once per frame
    void Update()
    {

    }
    IEnumerator SceneSwitch1()
    {
    print(Time.time);
    yield return new WaitForSeconds(3);
    print(Time.time);
    SceneManager.LoadScene("Auditions", LoadSceneMode.Single);
    }

    public void OnMouseDown()
    {
    // Start Game Button Script
    if (buttonID == 0)
    {
    anim.SetTrigger("StartGame");
    animation.SetTrigger("StartGame");
    StartCoroutine(SceneSwitch1());
    startMenu.transform.position = (new Vector3(0, 15, 0));
    }
    // Option Button Script
    else if (buttonID == 1)
    {
    startMenu.transform.position = (new Vector3(0, 15, 0));
    optionMenu.transform.position = (new Vector3(0, 3, 0));
    }
    // Credit Button Script
    if (buttonID == 2)
    {

    anim.SetTrigger("SetCredits");
    animation.SetTrigger("SetCredits");
    Instantiate(headCredits, new Vector3(0.8f, 1.5f, 0), Quaternion.identity);
    startMenu.transform.position = (new Vector3(0, 15, 0));
    }
    // Quit Game Script
    if (buttonID == 3)
    {
    Application.Quit();
    }
    // Exit to main Menu Script
    if (buttonID == 4)
    {
    anim.ResetTrigger("StartGame");
    anim.ResetTrigger("SetCredits");
    animation.ResetTrigger("StartGame");
    animation.ResetTrigger("StartGame");
    optionMenu.transform.position = (new Vector3(0, 15, 0));
    startMenu.transform.position = (new Vector3(0.69f, 4.75f, 0));
    MusicOn.transform.position = (new Vector3(-0.98f, 13.68f, 0));
    isMusicOff.transform.position = (new Vector3(-0.98f, 15, 0));
    }
    // Lower Volume Script
    if (buttonID == 5)
    {
    VolumeToggle();

    }
    // Raise Volume Script
    if (buttonID == 6)
    {

    }
    // Mute Music Button
    if (buttonID == 7)
    {
    AudioListener.volume = 0;
    Destroy(gameObject);
    Instantiate(isMusicOff, new Vector3(-1.1f, 4.11f, 0), Quaternion.identity);
    }
    // Unmute Music Button
    if (buttonID == 8)
    {
    Destroy(this.gameObject);
    Instantiate(MusicOn, new Vector3(-1.29f, 4.13f, 0), Quaternion.identity);
    AudioListener.volume = 1;
    }
    }
    public AudioSource menuMusic;
    [SerializeField]
    public GameObject MusicOn;
    public GameObject isMusicOff;
    public GameObject isVolumePlus;
    public GameObject isVolumeMinus;

    void VolumeToggle()
    {
    if (minusVolume == false)
    {
    menuMusic.volume = 1;
    }
    }
    }
     
    Last edited: Nov 18, 2019
  4. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
    Please use the "insert code" button when adding code into your posts, it makes it much easier to read.

    I assume you're talking about this piece of code?

    Code (CSharp):
    1. // Unmute Music Button
    2. if (buttonID == 8)
    3. {
    4. Destroy(this.gameObject);
    5. Instantiate(MusicOn, new Vector3(-1.29f, 4.13f, 0), Quaternion.identity);
    6. AudioListener.volume = 1;
    7. }
    This seems like a strange way to do things in my opinion. You're using 1 script which deals with multiple buttons code and I assume you're attaching it to each button and giving them a different "buttonID".

    It would be better to have a "ButtonController" script which references all your different buttons so it can enable and disable them, and then a different script for each button (or they could reference your buttonController and have all the button methods there). I see you're using OnMouseDown so I assume you're using gameobjects with colliders rather than UI buttons. UI buttons allow you to assign a function to a buttons OnClick event and if you did that you could have each button call your "ButtonController" script and call their own respective methods.

    In any case, I think it's always better to have UIController or ButtonController script that manages all of the enabling/disabling (Using SetActive() ) of your UI elements instead of each of your buttons Instantiating and Destroying each other.

    By the way, I don't think any code will run after destroying a gameobject? so in that snippet I wouldn't expect the instantiate or audiolistener lines to actually work. Destroying should be the last thing you do.
     
  5. heff2013

    heff2013

    Joined:
    Nov 1, 2019
    Posts:
    3
    hi sorry i'm really new to the forum/unity coding, don't know how the page works just need help with answers. I appreciate it. This is also our first year on C# so we'll try that