- Home /
Deleting life object from scene
Hi, im creating a game where to players spawn in and run around over little pads, giving them points. there is a timer going and when the timer runs out the game stops and the player with the most points win, at the moment once the time runs out a new scene is loaded and says which player won and the score they had, then it goes back to the game scene and starts again. i added 3 objects in the top right of the screen and 3 in the top left, these represent lives. once the round ends, i want the losing player to lose a life, i cannot get this working however. at the moment i only have it coded that if the playerOne loses, then they lose a life, this works for the first life, but not the second or third. these are the relevant scrips, how should i fix this issue?
Player Lives
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
public class PlayerLives : MonoBehaviour
{
public static bool playerTwoRound1 = false;
public static bool playerTwoRound2 = false;
public static bool playerTwoRound3 = false;
public GameObject playerOneLifeOneObj;
public GameObject playerOneLifeTwoObj;
public GameObject playerOneLifeThreeObj;
void Update()
{
if(Coutndown.playerTwoWins)
{
playerTwoRound1 = true;
Coutndown.playerTwoWins = false;
}
if(Coutndown.playerTwoWins && playerTwoRound1)
{
playerTwoRound2 = true;
Coutndown.playerTwoWins = false;
}
if(Coutndown.playerTwoWins && playerTwoRound3)
{
playerTwoRound3 = true;
Coutndown.playerTwoWins = false;
}
if(playerTwoRound1)
{
Destroy(playerOneLifeOneObj);
}
if(playerTwoRound2)
{
Destroy(playerOneLifeTwoObj);
}
if (playerTwoRound3)
{
Destroy(playerOneLifeThreeObj);
}
}
}
This script is what gets the life objects and is suppose to delete them, the other script is the Countdown Script
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Coutndown : MonoBehaviour
{
float timeLeft = 3.0f;
float gameTimeLeft = 15.0f;
float waitForWin = 2.0f;
public Text text;
public Text gameTime;
public Text playerOneScore;
public Text playerTwoScore;
public GameObject spawnObject;
PlayerSpawn spawnScript;
public GameObject playerOne;
PlayerController playerOneScript;
Knockback playerOneKnockback;
public GameObject playerTwo;
PlayerController playerTwoScript;
Knockback playerTwoKnockback;
public GameObject gameManager;
Score scoreScript;
public bool startGame;
public bool endGame;
public static bool playerTwoWins = false;
void Start()
{
spawnScript = spawnObject.GetComponent<PlayerSpawn>();
playerOneScript = playerOne.GetComponent<PlayerController>();
playerTwoScript = playerTwo.GetComponent<PlayerController>();
playerOneKnockback = playerOne.GetComponent<Knockback>();
playerTwoKnockback = playerTwo.GetComponent<Knockback>();
scoreScript = gameManager.GetComponent<Score>();
startGame = false;
endGame = false;
}
void Update()
{
if (spawnScript.playersSpawned)
{
StartCountdown();
}
}
void StartCountdown()
{
timeLeft -= Time.deltaTime;
text.text = Mathf.Round(timeLeft).ToString();
if (timeLeft < 1)
{
text.text = "GO!";
}
if (timeLeft < 0)
{
playerOneScript.speed = 10f;
playerTwoScript.speed = 10f;
playerOneKnockback.started = true;
playerTwoKnockback.started = true;
text.text = "";
StartGame();
}
}
void StartGame()
{
gameTimeLeft -= Time.deltaTime;
gameTime.text = Mathf.Round(gameTimeLeft).ToString();
if(gameTimeLeft < 0)
{
EndGame();
}
}
void EndGame()
{
playerOneScript.speed = 0f;
playerTwoScript.speed = 0f;
gameTime.text = "";
playerOneScore.text = "";
playerTwoScore.text = "";
text.text = "Times Up!";
text.fontSize = 150;
if (scoreScript.playerOneScore > scoreScript.playerTwoScore)
{
waitForWin -= Time.deltaTime;
if (waitForWin < 0)
{
SceneManager.LoadScene("PlayerOneWin");
}
}
else if (scoreScript.playerTwoScore > scoreScript.playerOneScore)
{
waitForWin -= Time.deltaTime;
if (waitForWin < 0)
{
playerTwoWins = true;
SceneManager.LoadScene("PlayerTwoWin");
}
}
else if (scoreScript.playerOneScore == scoreScript.playerTwoScore)
{
waitForWin -= Time.deltaTime;
if (waitForWin < 0)
{
SceneManager.LoadScene("Draw");
}
}
}
}
This script sets timers throughout the game, like once the players spawn in, a 3 second countdown starts, then another timer starts which is how long the game goes for. in the EndGame function i say if the playerTwo score is higher than the player two wins, and the playerTwoWins bool is set to true, which is what im using to delete the objects in the first script.