- Home /
Question by
adastro17 · Jul 07, 2021 at 12:31 PM ·
setactiveinput.getkey
How to toggle an game object once and never again using set active method.,Toggle an object once and never be able to toggle it again
So I want to make my code to where i can only make myTarget
appear once and cant disappear after that. But that's not what's happening. Every time i press Q
it appears then when i press Q
again it disappears. I know i have to put in a if myTarget
is active then... but i don't know what to put in after that.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class inv : MonoBehaviour
{
public GameObject m_Target;
void Start()
{
m_Target.SetActive(false);
}
void Update ()
{
if ( Input.GetKeyDown(KeyCode.Q))
{
m_Target.SetActive(!m_Target.activeSelf);
}
}
}
Comment
As @Borjka said, you just have to use true instead of !m_Target.activeSelf.......this here makes the gameobject to go in the opposite of the current state....which means it triggers it on when it's off and vice versa
Answer by Borjka · Jul 07, 2021 at 12:45 PM
@adastro17 This way it should be working the way you want
public class inv : MonoBehaviour
{
public GameObject m_Target;
void Start()
{
m_Target.SetActive(false);
}
void Update ()
{
if ( Input.GetKeyDown(KeyCode.Q))
{
m_Target.SetActive(true);
}
}
}