Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity Score System

Discussion in 'UGUI & TextMesh Pro' started by leadzeplin, Jun 15, 2015.

  1. leadzeplin

    leadzeplin

    Joined:
    Jun 7, 2015
    Posts:
    3
    I am trying to create a score system on unity where whenever the game object tagged "Mean" hits an empty game object (Both have a rigid body and box collider attached to it) the score goes up by one. Here is my code. For some reason nothing happens when I run it, but no errors come up. Thanks

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Scorer : MonoBehaviour {
    5.  
    6.     public static int totalScore;
    7.     public static float runScore;
    8.    
    9.  
    10.     void Awake()
    11.     {
    12.         totalScore = 0;
    13.         runScore = 0;
    14.     }
    15.    
    16.    
    17.  
    18.     void OnGUI()
    19.     {
    20.        
    21.  
    22.         GUI.Label(new Rect(0, 4, 200, 50), "Total Score: " + totalScore);
    23.        
    24.     }
    25.     // Use this for initialization
    26.     void Start () {
    27.    
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update () {
    32.    
    33.     }
    34.  
    35.  
    36.     void OnCollisionEnter2D(Collision2D coll) {
    37.         if(coll.gameObject.tag == "Enemy")  {
    38.  
    39.             totalScore ++;
    40.         }
    41.  
    42.     }
    43.  
    44. }
    45.  
     
  2. LyveIG

    LyveIG

    Joined:
    Jan 8, 2015
    Posts:
    15
    You're testing for 2D collisions. Are you sure that you added BoxCollider2D and RigidBody2D? Otherwise it won't work. Just asking, you only said BoxCollider and RigidBody, these are 3D objects.