Search Unity

Deactivate/Toggle Minimap

Discussion in 'Scripting' started by Hawk0077, Dec 10, 2019.

  1. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    590
    Hi, I have a Minimap game object and a SelectorUI.

    When the SelectorUI is active I want to Deactivate the Minimap to stop it showing over the SelectorUI.
    Then when I turn the SelectorUI off I want to reactivate the Minimap.

    I have created this code from bits I have found on the internet but it doesnt work.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class ToggleMiniMap : MonoBehaviour
    6. {
    7.     GameObject Minimap;
    8.     public GameObject SelectorUI;
    9.  
    10.     void Update()
    11.     {
    12.         if (SelectorUI == true)
    13.         {
    14.             Minimap.SetActive(false);
    15.         }
    16.         else
    17.         {
    18.             Minimap.SetActive(true);
    19.         }
    20.     }
    21. }
    Any ideas what is wrong with it.

    Im new to coding so this is guesswork at best.

    Any assistance appreciated, thanks
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    625
    Hi @Hawk0077,
    Can you detail your use case a bit more? Do you have some button in UI which you would want to use to toggle Minimap on and off, you do you actually want to use a GameObject's state to toggle it on and off?
     
  3. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    590
    I dont want to use a button. I want to use a script which checks for the SelectorUI. When the SelectorUI becomes active (I press a joystickbutton to do that.

    ... Then I want the Minimap to deactivate.


    Then when I use the joystickbutton again to deactivate the SelectorUI. The Minimap should then reactivate and show again.
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    625
    Ok, well now you are testing if the object is true, and it will return true always when it's assigned. Instead, test if the object is active, i.e do something like this:

    Code (CSharp):
    1. public GameObject minimap;
    2. public GameObject selectorUI;
    3.  
    4. void Update()
    5. {
    6.     if (minimap != null && selectorUI != null)
    7.     {
    8.         if (selectorUI.activeSelf)
    9.         {
    10.             minimap.SetActive(true);
    11.         }
    12.         else if (!selectorUI.activeSelf)
    13.         {
    14.             minimap.SetActive(false);
    15.         }
    16.     }
    17. }
    So now that minimap will get deactivated / activated based on the state (disabled/enabled) of that selectorUI GameObject. You can probably adapt this to your use case.
     
    Hawk0077 likes this.
  5. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    590
    Thanks Olmi, I appreciate it. I will give it a go shortly. And post back here but then theres no need to respond as you've done enough and I appreciate it. Many thanks
     
  6. Hawk0077

    Hawk0077

    Joined:
    Nov 6, 2017
    Posts:
    590
    Worked opposite so I just changed the true/false statements and it worked fine.

    Code (CSharp):
    1.             if (selectorUI.activeSelf)
    2.             {
    3.                 minimap.SetActive(false);
    4.             }
    5.             else if (!selectorUI.activeSelf)
    6.             {
    7.                 minimap.SetActive(true);
    8.             }
    Thanks, I appreciate it. Awesome.
     
    Olmi likes this.
unityunity