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 /
avatar image
1
Question by lm97skater · Dec 27, 2016 at 07:37 PM · scripting beginnerbeginnertriggersscore system

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.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I Have made a movement script for my pacman sprite. But animations keep playing even when there is no input. ans when i drag my sprite around in the scene it keeps trying to return to its original position. 0 Answers

How to move the object to where the object is already pointing to? 1 Answer

Disabling character controller after collision 2 Answers

Rotate the character movement with the character 1 Answer

Check if a pre-determinated area has particles in it? 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