Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by KisoraAssets · Jul 22, 2019 at 02:41 PM · 3dscoreendgamemultiple-values

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;
         }
     }  
     
 }
 





Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

205 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

how put 3d object in front of android camera? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges