Search Unity

C# scoring

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

  1. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    For some reason i can't set the code to stop the score if my player hit the obstacle. The player should only score if the successfully pass the obstacle(hitting the trigger). A little help would be appreciated. Thanks everyone in advance.. Also if anyone can throw in a method to enable a multiplier after successfully passing 3 obstacles. Would be much appreciated, i have never tried making a multiplier for a game. Here is what i have.


    Code (CSharp):
    1. public static int playerScore = 0;        //This is the player's score.
    2.     public int oScore = 10;              
    3.     private GameObject scoreText;
    4.     public int noScore = 0;
    5.     // Use this for initialization
    6.     void Start () {
    7.     scoreText = GameObject.Find("scoreNumber");
    8.     }
    9.    
    10.     //COLLISIONS
    11.     void OnTriggerEnter(Collider col)
    12.     {  
    13.         //If we collide with a trigger, increase player's score.
    14.         if(col.gameObject.name == "Pointgiver")
    15.         {
    16.             Destroy(col.gameObject);
    17.             playerScore += oScore;
    18.  
    19.         }
    20.         if (col.gameObject.tag == "obstacle")
    21.                        
    22.         playerScore = 0;
    23.     }
    24.  
    25.  
    26.     void OnGUI()
    27.  
    28.     {
    29.         scoreText.guiText.text = playerScore.ToString();
    30.  
    31.     }
    32. }
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    Your code looks fine, but perhaps you should place a print() or debug.log() in the function enter trigger. Monitor if it is hitting something, what it is and how many times.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    does the player have a rigidbody? collision require something involved to have one in order for the physics engine to pick up on the interaction.

    is the obstacle's collider set to "trigger"? I would envisage obstacles should cause the player to "crash" i.e. stop and would be colliders, not trigger colliders... basically can you explain the components you have on the obstacle?


    for the multiplier, you'll need to start tracking the number of obstacles the player has passed (an int) and update variable in the "PointGiver" code above, and another for the current multiplier, then instead of "playerScore += x" you create a function "AddScore(..)" where you can setup the logic i.e. if or a case statement which adds whatever if the "passed obstacles" variable is whatever value you desire etc.
     
  4. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    Try this.

    Attach this To Your Score Text.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class Score : MonoBehaviour
    7. {
    8.     public static int score;
    9.  
    10.  
    11.     Text text;
    12.  
    13.  
    14.     void Start()
    15.     {
    16.         text = GetComponent<Text> ();
    17.  
    18.         score = 0;
    19.  
    20.        
    21.  
    22.  
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         if (score < 0)
    28.             score = 0;
    29.  
    30.         text.text = "" + score;
    31.     }
    32.  
    33.     public static void AddPoints (int pointsToAdd)
    34.     {
    35.         score += pointsToAdd;
    36.        
    37.  
    38.     }
    39.  
    40.  
    41.  
    42.     public static void Reset()
    43.     {
    44.  
    45.        
    46.         score = 0;
    47.     }
    48. }
    49.  
    And this To Your Object.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class Score_System : MonoBehaviour {
    7.  
    8.     public int pointsToAdd;
    9.  
    10.  
    11.  
    12. void OnTriggerEnter(Collider col)
    13.     {
    14.      
    15.         if(col.gameObject.name == "Pointgiver")
    16.         {
    17.             Score.AddPoints(pointsToAdd);
    18.             Destroy(col.gameObject);
    19.  
    20.          
    21.         }
    22.     }
    23. }
     
  5. amichael

    amichael

    Joined:
    Mar 4, 2015
    Posts:
    48
    @LeftyRighty the obstacles are triggers, and the player does have a rigid body. Also not too sure what you mean about the multiplier, could you give an example? @shawnrevels I tried what you were talking about, and nothing was happening with that script. I might have misunderstood what you meant, because i attached where you said. I really appreciate everything guys.
     
  6. garrido86

    garrido86

    Joined:
    Dec 17, 2013
    Posts:
    233
    You are checking against the Gameobjects name, that's not good because this is prone for errors. For example if you spawn new instances of Pointgiver, they will have the name "Paintgiver (Clone X)" and then your check fails.
    Instead give Pointgiver a Tag like "Bonus" and check for that.
    Code (CSharp):
    1. if(col.gameObject.tag == "Bonus")