how to enable a component through script ?

So, while coding a script for the player to respawn after dying, it worked, though, while respawning, all the Player components got disabled, wich is weird, but to fix that, i wanted to enable individually each components through scripts, i tried different syntaxes but always had errors, how do you enable components through scripts please?

what component are you tying to change?, some can’t be disabled\enabled.
what was the code you got errors on and what errors did you get?

Can you copy paste the first error?

Find out why it’s doing that instead of trying to fix the symptom.
Maybe post your death/respawn code

these are the two pieces of code i used:

public Transform respawnPoint;
    public static LevelManager instance;
    public GameObject playerPrefab;

    private void Awake()
    {
        instance = this;
    }

    public void Respawn()
    {
        Instantiate(playerPrefab, respawnPoint.position, Quaternion.identity);
       
    }

and

    void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.CompareTag("Hazards"))
        {
           Destroy(gameObject);
           LevelManager.instance.Respawn();
        }
    }

the error was “‘Component’ does not contain a definition for ‘enabled’ and no accessible extension method ‘enabled’ accepting a first argument of type ‘Component’ could be found (are you missing a using directive or an assembly reference?)”

and the components are:
Box Collider2D, Circle Collider2D, and all the scripts on it

sorry, my problem may be dumb

gameObject.GetComponent(“nameComponent”).enabled = false; :

gameObject.SetActive(bool); :

Thank you very much ! :smile:

1 Like