1
\$\begingroup\$

I'm new to unity and creating my first game to learn more about it. Now i got a problem wich i click on a button and sometimes another button active, for example: i press "Options" on pause menu and it active "Restart" or the "Main Menu". if someone can help me, I appreciate it. I set them up like this, if there's any error or need more info please let me know. And in the first image i tried split in 2 canvas, but didn't work. P.S.: It happens even with buttons with no actions or scripts on them.

enter image description here

enter image description here

enter image description here enter image description here

enter image description here

public void OnPause()
{
    Time.timeScale = 0;
    isPaused = true;
    unPauseButton.SetActive(true);
    pauseButton.SetActive(false);
    PauseGameMenu.SetActive(true);

    GameObject.Find("PowerUpSpawner").GetComponent<PowerUpManager>().enabled = false;

}    
public void UnPause()
{
    Time.timeScale = 1;
    pauseButton.SetActive(true);
    unPauseButton.SetActive(false);
    PauseGameMenu.SetActive(false);
    GameObject.Find("PowerUpSpawner").GetComponent<PowerUpManager>().enabled = true;
    isPaused = false;


}
\$\endgroup\$
  • \$\begingroup\$ Hello, could you give us the part of your code where you define the button actions please ? \$\endgroup\$ – Shashimee Jul 28 '17 at 14:06
  • \$\begingroup\$ yeap, i edited the post take a look, but i forget to add it happens even in buttons wich don't have a actions defined. Like the arrows in the canvas i only set up them. \$\endgroup\$ – Lucas Gomes Jul 28 '17 at 14:13
  • \$\begingroup\$ Okay. These are the actions that the buttons do, but how/where did you tell your program exactly "This button Pause should do this PauseAction" etc. ? There can be done in the code by gettint the button component and adding it a OnClick function, or in the inspector directly. \$\endgroup\$ – Shashimee Jul 28 '17 at 14:18
  • \$\begingroup\$ Sorry my bad, I add it in the inspector directly \$\endgroup\$ – Lucas Gomes Jul 28 '17 at 14:21
  • \$\begingroup\$ No problem, could you provide some picture of it ? :) Also, I didn't see any EventSystem object in your Canvas, it is necessary to correctly handle inputs like buttons \$\endgroup\$ – Shashimee Jul 28 '17 at 14:25
1
\$\begingroup\$

If you made sure that all of you functions are correctly mapped to the buttons (PauseAction for the Pause button etc.) then it must be your EventSystem object. You only need one for one canvas and having multiple can create conflits. You Canvas tree could look like this (I'm at work so no Unity image sorry) :

Canvas

  • PauseButton
  • MenuButton
  • EventSystem

You can just add the EventSystem as it is, no special configuration is necessary for your purposes.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

You can reference the object that you want to activate as a public field.

public GameObject Target;
private Button button;
private void Awake()
{
    // Find your button here.
    button = GetComponent<Button>();
    // Handle button event.
    button.onClick.AddListener(ToggleTarget);
}
private void ToggleTarget()
{
    // Activate/deactivate the target.
    Target.SetActive(!Target.activeSelf);
}

Add something like the code above, then you can set your target object in your inspector. You can use the same code for all button objects with same functionality.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.