Why doesn't deactivate and activate work, is this because of Canvas

I’ll explaine my problem:
I’f got an object on a plane and an object in a canvas. It should be that when I press a key on the keyboard that the object A is going to be deactivated and the object B be activated, this works fine, but when I wan’t to change it back to object A to be activated and object B to be deactivated this doesn’t work?
It should be so simple but it seams that it doesn’t, is this because of the canvas?

Object A is on the plane and object B is on the Canvas.

This is my script:

if (Input.GetKeyDown (KeyCode.A) && A == true)
        {
            A.SetActive (false);
            B.SetActive (true);
        }
        if (Input.GetKeyDown (KeyCode.A) && A != true) // or A == false, even tried B == true
        {
            A.SetActive (true);
            B.SetActive (false);
        }

A == true does not mean A is active, rather A exists and has not been destroyed.
You’re looking for A.activeSelf or A.activeInHierarchy.

Tried both of them but seams to be the same problem.

You probably want an else if statement in there or otherwise rearrange your logic to not run both cases in one go.

You’re checking to see if A is active, turning it off, and then immediately checking if it’s inactive and turning it back on again.

I tried an else if statement, but then when pressing the key on the keyboard the object gets back.

maybe that should be the problem, how can I fix this. I do need to use the same key A to switch back.

Stop doing it :slight_smile:

If it’s just a toggle:

if (Input.GetKey(....))
{
    A.SetActive(!A.activeSelf);
    B.SetActive(!B.activeSelf);
}
1 Like

No way…
Couldn’t found out that one.
This really works, thanks a lot KelsoMRK, you are awsome. :slight_smile: