- Home /
How to make a simple scoring system with multiple triggers?
I'm very new to Unity, and game dev in general, so if possible, talk to me like I'm two?
I'm creating a simple game where there are four coloured balls and four coloured holes. The objective is to get the balls into the corresponding holes.
Here's what I want the scoring system to be like: Get a ball into the right hole: +1 Get a ball into the wrong hole: -1
I have one script for each trigger, they all look the same (besides for the name of the colours) but this is the one for the green trigger:
 void Start ()
 {
     Count = 0;
     Score.text = "Score: " + Count.ToString ();
 }
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.CompareTag ("Green"))
     {
          Count = Count + 1;
          Score.text = "Score: " + Count.ToString ();
          other.gameObject.SetActive (false);
     }
     if (other.gameObject.CompareTag ("Blue"))
     {
         Count = Count - 1;
         Score.text = "Score: " + Count.ToString ();
         other.gameObject.SetActive (false);
     }
     if (other.gameObject.CompareTag ("Red"))
     {
         Count = Count - 1;
         Score.text = "Score: " + Count.ToString ();
         other.gameObject.SetActive (false);
     }
     if (other.gameObject.CompareTag ("Yellow"))
     {
         Count = Count - 1;
         Score.text = "Score: " + Count.ToString ();
         other.gameObject.SetActive (false);
     }
  }
Expected behavior: The score gets increased when I get a ball into the right hole, and decreased when getting one into the wrong hole, making the maximum score 4, and minimum -4.
Examined behavior: It works fine for the first ball, but then things start to get weird. If I get the first ball right, the score wont increase anymore (it will stay at 1). Same goes for getting one in the wrong hole (it will stay at -1).
Note: I'e also tried adding another ball to see how it would behave if I got two of the same coloured balls in the right hole. This seemed to work fine, the score went to 2 but the rest of it still didn't work. This is why I'm assuming it's a problem with the multiple triggers.
Answer by Hellium · Dec 27, 2016 at 07:41 PM
You need to manage the score from an external entity. Create a new GameObject and attach the following ScoreManagerscript.
 using UnityEngine;
 using UnityEngine.UI;
 public class ScoreManager : MonoBehaviour
 {
     // From the inspector, drag & Drop the GameObject holding the Text component used to display the score
     [SerializeField]
     private Text scoreText ;
     
     private int score;
     
     public int Score
     {
         get { return score ; }
         set
         {
             score = value ;
             scoreText.text = "Score: " + score.ToString ();
         }
     }
     
     private void Awake()
     {
         Score = 0;
     }
 }
Then, on each hole, you have to reference the Score Manager
 public class Hole: MonoBehaviour
 {
     // From the inspector, drag & Drop the GameObject holding the ScoreManager created just before
     [SerializeField]
     private ScoreManager scoreManager ;
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag ("Green"))
         {
             scoreManager.Score++ ;
             other.gameObject.SetActive (true);
         }
         else if (other.gameObject.CompareTag ("Blue"))
         {
             scoreManager.Score--;
             other.gameObject.SetActive (false);
         }
         else if (other.gameObject.CompareTag ("Red"))
         {
             scoreManager.Score--;
             other.gameObject.SetActive (false);
         }
         else if (other.gameObject.CompareTag ("Yellow"))
         {
             scoreManager.Score--;
             other.gameObject.SetActive (false);
         }
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                