end game when multiple scores all hit zero
I have a level in my game where you operate a crane and drop off different shipping containers. There are 3 different colored containers and each color is measured with a score (ex: Blue containers: 3, Red Containers: 3, etc) My score code works properly BUT i want to end the game once all of the scores hit zero. I haven't found any other questions on the forums personally that ask how to end a game when multiple score values all equal the same thing.
Here's my code, i'm not the best at coding but if it works great then i'm not gonna fight it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ContDropoff : MonoBehaviour
{
public GameObject contb;
public GameObject contr;
public GameObject conty;
public GameObject spotlight;
public int BlueCount;
public int RedCount;
public int YellowCount;
public Text BlueText;
public Text RedText;
public Text YellowText;
public Text VictoryScreen;
void Start ()
{
BlueCount = 3;
RedCount = 3;
YellowCount = 3;
BlueText.text = "Blue Containers left: " + BlueCount;
RedText.text = "Red Containers left: " + RedCount;
YellowText.text = "Yellow Containers left: " + YellowCount;
}
void Update()
{
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "Blue Goal")
{
BlueCount -= 1;
contb.SetActive(false);
spotlight.SetActive(true);
BlueText.text = "Blue Conteiners left: " + BlueCount;
}
if (col.gameObject.name == "Red Goal")
{
RedCount -= 1;
contr.SetActive(false);
spotlight.SetActive(true);
RedText.text = "Red Containers left: " + RedCount;
}
if (col.gameObject.name == "Yellow Goal")
{
YellowCount -= 1;
conty.SetActive(false);
spotlight.SetActive(true);
YellowText.text = "Yellow Containers left: " + YellowCount;
}
}
}
Answer by KisoraAssets · Jul 22, 2019 at 02:50 PM
one way i thought about doing it might just be making a score value for the total amount of containers and once that hits zero; boop, im done. End the game. But i feel like there might be a simpler way.
Answer by Vega4Life · Jul 22, 2019 at 04:56 PM
Here is a simple idea that uses mark ups on your goal objects.
// Place on your goal gameObject and set the goal type (which is the color)
public class Goal : MonoBehaviour
{
public enum GoalType { Blue, Red, Yellow }
// Set in inspector
[SerializeField] GoalType goalType;
public GoalType GetGoalType { get { return goalType; } }
}
Then the rest of your code would look something like this:
public class ContDropoff : MonoBehaviour
{
public GameObject contb;
public GameObject contr;
public GameObject conty;
public GameObject spotlight;
public int BlueCount;
public int RedCount;
public int YellowCount;
public Text BlueText;
public Text RedText;
public Text YellowText;
public Text VictoryScreen;
void Start()
{
BlueCount = 3;
RedCount = 3;
YellowCount = 3;
BlueText.text = "Blue Containers left: " + BlueCount;
RedText.text = "Red Containers left: " + RedCount;
YellowText.text = "Yellow Containers left: " + YellowCount;
}
void OnCollisionEnter(Collision col)
{
Goal goal = col.gameObject.GetComponent<Goal>();
if (goal)
{
GoalCompleted(goal.GetGoalType);
}
}
// Simplification so you don't have so much junk in the collision
void GoalCompleted(Goal.GoalType goal)
{
contb.SetActive(false);
spotlight.SetActive(true);
switch (goal)
{
case Goal.GoalType.Blue:
BlueCount -= 1;
BlueText.text = "Blue Conteiners left: " + BlueCount;
break;
case Goal.GoalType.Red:
RedCount -= 1;
RedText.text = "Red Containers left: " + RedCount;
break;
case Goal.GoalType.Yellow:
YellowCount -= 1;
YellowText.text = "Yellow Containers left: " + YellowCount;
break;
}
CheckGameComplete();
}
void CheckGameComplete()
{
if (BlueCount == 0 && RedCount == 0 && YellowCount == 0)
{
// Game Ended
}
}
}
Pretty straightforward. Also, don't have empty Updates, Starts, Awakes. It's a waste of resources ( (granted its tiny, but its worth getting in the habit of not having them).
Your answer
Follow this Question
Related Questions
show score and keep score on screen until start new game? 0 Answers
How do you add player lives and end the game? 0 Answers
Item is only picked up when it is moving 0 Answers
Ball won't stop bouncing 1 Answer