SetActive is just working if i press twice in the first time
Hi guys,
I made a script which activate/deactivates a Gameobject. The script itself is working fine, but I need to press ESC twice at the first time. After the gameobject is disabled once, I just need to press one time ESC. Is there a way that i need to press ESC just one time in the beginning?
public class EnableCanHolder : MonoBehaviour {
public GameObject CanHolder;
int CanHolderVisible = 1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
public void Update () {
if (Input.GetKeyDown(KeyCode.Escape) && CanHolderVisible == 1)
{
CanHolder.SetActive(true);
CanHolderVisible = 2;
}
else if (Input.GetKeyDown(KeyCode.Escape) && CanHolderVisible == 2)
{
CanHolder.SetActive(false);
CanHolderVisible = 1;
}
}
}
Answer by InfiniBuzz · Oct 19, 2016 at 02:50 PM
set int CanHolderVisible = 1;
to int CanHolderVisible = 2;
at the beginning, unless the CanHolder is not active in the beginning. then you would need to swap the if's ==1 and ==2 around.
Answer by tom_coursow · Oct 19, 2016 at 03:56 PM
You could also ask for the current active state of canholder in Start() method and then assign the depending value to CanHolderVisible. That way no matter if your object would be active or inactive at first it would always work.