Deactivate/Toggle Minimap

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.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ToggleMiniMap : MonoBehaviour
{
    GameObject Minimap;
    public GameObject SelectorUI;

    void Update()
    {
        if (SelectorUI == true)
        {
            Minimap.SetActive(false);
        }
        else
        {
            Minimap.SetActive(true);
        }
    }
}

Any ideas what is wrong with it.

Im new to coding so this is guesswork at best.

Any assistance appreciated, thanks

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?

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.

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:

public GameObject minimap;
public GameObject selectorUI;

void Update()
{
    if (minimap != null && selectorUI != null)
    {
        if (selectorUI.activeSelf)
        {
            minimap.SetActive(true);
        }
        else if (!selectorUI.activeSelf)
        {
            minimap.SetActive(false);
        }
    }
}

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.

1 Like

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

Worked opposite so I just changed the true/false statements and it worked fine.

            if (selectorUI.activeSelf)
            {
                minimap.SetActive(false);
            }
            else if (!selectorUI.activeSelf)
            {
                minimap.SetActive(true);
            }

Thanks, I appreciate it. Awesome.

1 Like