Unity get gameobject disabled

Asked

Viewed 671 times

0

My player has shield that is disabled in it, and I have a game manager wants to take care for when you can activate the shield or do not put when I instate the player in the scene the game manager does not find the component of the shield, this is because it is disabled, If active he finds more when the time of the shield is over he returns to not find anyone else knows what can be ? I’m using:

   void Awake()
{
    if (_escudo != null)
    {
        _escudo =  GameObject.Find("Escudo"); //ele acha porque estou iniciando com o escudo ativado 

    }
    else
    {
        InvokeRepeating("checkComponentes", 0f, 2f);
    }
}

here the routine function:

 if (_component.input != 0 && _time >= _tempoDuracaoPoderes && _timeDelay <= 0)
    {// recebe o aperto do botao e verifica outras condições antes de ativar 

        StartCoroutine(EscudoForca()); // responsavel por controlar o tempo do escudo
        _buttonescudoForca.enabled = false;
        _time = 0;                    
    }
    else
    {
        _time += Time.deltaTime;
        _timeDelay -= Time.deltaTime;
    }

}
IEnumerator EscudoForca()
{
    _escudo.SetActive(true); //escudo ativo
    _uiEscudo.SetActive(true);// efeito de lente ativa
    statusBar.fillAmount = 1; 
    anim.SetBool("ativado", true);
    _timeDelay = 5.0f;
   // _escudo = GameObject.Find("Escudo");

    yield return new WaitForSeconds(_tempoDuracaoPoderes);

   // _escudo = GameObject.Find("Escudo");
    _escudo.SetActive(false);
    _uiEscudo.SetActive(false);
    statusBar.fillAmount = 0;
    anim.SetBool("ativado", false);




}

The issue is I can get Gameobject disabled to be able to activate later, as I already do more if the PLAYER is on the screen if instantiated does not work. Thanks in advance...

1 answer

1

The Gameobject.Find function only returns active objects, to return inactive objects in the scene uses the function Resources.Findobjectsoftypeall (Very careful to use this, it consumes a lot of processing)

If your shield object has a script it is easy, otherwise it is best to create a manager who has the reference of your object somewhere to have this access;

Another way to access the parent gameobject using Getcomponentsinchildren()

There is a similar question: LINK

Browser other questions tagged

You are not signed in. Login or sign up in order to post.