I have completed the Space Shooter tutortioal and currently wanting to impliment a lives system.
I’m open to other ways of doing this but so far to avoid re doing the current meachanics I am using IEnumeratore to set the player to active and false. I am using it in the DestroyByContact script.
My goal is to:
- Set Player active to false
- wait 3 seconds
- Set player active to true but with no collider (shot term invincibility)
- wait 3 seconds
- Set Player collider to true
For some reason the IEnumeratore just does not work and won’t trigger the code after the second wait.
If I just have log out put I can get through the two wait times without issue but as soon as I bring in the setactive and collider true/false it breaks.
There are no errores in the console.
IEnumerator Function:
IEnumerator RespawnPlayer()
{
Debug.Log("Dead");
player.SetActive(false);
yield return new WaitForSeconds(2);
Debug.Log("respawn");
player.SetActive(true);
playerCollider.enabled = false;
yield return new WaitForSeconds(2);
Debug.Log("Collider On");
playerCollider.enabled = true;
}
Full Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyByContact : MonoBehaviour
{
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
private GameController gameController;
private bool gameOver;
private GameObject player;
private Collider playerCollider;
void Start()
{
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if(gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent<GameController>();
}
if(gameController == null)
{
Debug.Log("Cannot find 'Gamecontroller' script");
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Boundary") || other.CompareTag("Enemy"))
{
return;
}
if (explosion != null)
{
Instantiate(explosion, transform.position, transform.rotation);
}
if(other.CompareTag("Player"))
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
gameOver = gameController.GameOver();
if(gameOver == false)
{
player = GameObject.FindGameObjectWithTag("Player");
playerCollider = player.GetComponent<Collider>();
StartCoroutine(RespawnPlayer());
return;
}
}
else // the else stops points being scored from cashing into an enemy/hazzard
{
gameController.AddScore(scoreValue);
}
Destroy(other.gameObject);
Destroy(gameObject);
}
IEnumerator RespawnPlayer()
{
Debug.Log("Dead");
player.SetActive(false);
yield return new WaitForSeconds(2);
Debug.Log("respawn");
player.SetActive(true);
playerCollider.enabled = false;
yield return new WaitForSeconds(2);
Debug.Log("Collider On");
playerCollider.enabled = true;
}
}
GameOver function from the GameController Script:
public bool GameOver()
{
if(lives > 0)
{
lives = lives - 1;
livesText.text = "X " + lives;
gameOver = false;
return false;
}
gameOverText.text ="Game Over";
totalScoreText.text = "Top Score: " + score;
gameOver = true;
return true;
}
Am I using the I enumerator in the wrong way?
Thanks