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

score system help C#

Discussion in 'Scripting' started by amichael, Jun 27, 2015.

  1. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    I have a scoring system put in place with triggers setup on my prefabs (obstacles) the player has to run through. It works, but the player still scores if they hit the obstacle. Any ideas on how to stop the scoring if they hit the obstacle? Also i want the score to multiply after a certain amount of times without hitting an obstacle. Here is what i have so far.
    Code (CSharp):
    1.  
    2.     public static int playerScore = 0;        //This is the player's score.
    3.     public int oScore = 10;              
    4.    
    5.     private GameObject scoreText;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.     scoreText = GameObject.Find("scoreNumber");
    10.     }
    11.    
    12.     //COLLISIONS
    13.     void OnTriggerEnter(Collider col)
    14.     {  
    15.         //If we collide with a trigger, increase player's score.
    16.         if(col.gameObject.name == "Pointgiver")
    17.         {
    18.             Destroy(col.gameObject);
    19.             playerScore += oScore;
    20.         }
    21.  
    22.     }
    23.    
    24.     void OnGUI()
    25.     {
    26.         scoreText.guiText.text = playerScore.ToString();
    27.  
    28.     }
    29. }
    30.  
     
  2. booiljoung

    booiljoung

    Joined:
    May 12, 2013
    Posts:
    57
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    What I did with my current project. I have the scoring trigger positioned after the obstacle, insuring that the player cannot hit the obstacle once the trigger is entered.