'How do I use SetActive to turn on my Player Object from my GameManager?
I'm writing an Asteroid clone and I am running up against an issue with re-spawning my player object after it is deactivated on collision with an asteroid. I run the SetActive(false) function to turn off the object from within the player object and when I try to use SetActive(true) from the GameManager to reactivate the Player object it doesn't work. Any help would be appreciated.
Here is the section of code in the Player script where I turn off the Player object:
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Asteroid")
{
_rigidbody.velocity = Vector3.zero;
_rigidbody.angularVelocity = 0.0f;
this.gameObject.SetActive(false);
GameManager.Instance.PlayerDeath();
}
and here is the section of the GameManager where I try to reactivate the Player object:
public void PlayerDeath()
{
this.lives--;
if (this.lives <= 0)
{
GameOver();
}
else
{
//Invoke(nameof(Respawn), this.respawnTimer);
StartCoroutine(Respawn(respawnTimer));
}
}
public IEnumerator Respawn(float f)
{
yield return new WaitForSeconds(f);
this.player.transform.position = Vector3.zero;
this.player.gameObject.SetActive(true);
}
Solution 1:[1]
Is your GameOver method will call IEnumerator Respawn?
if not then I think that's your problem, cause what I know so far is that
an if statement will executed and if it's True if not True then it will be an Else statement, in your case your IEnumerator was inside Else statement try to move it above.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ShockedCat |
