Search Unity

Scoring

Discussion in 'Scripting' started by Jacob8079, Jun 5, 2018.

  1. Jacob8079

    Jacob8079

    Joined:
    May 30, 2018
    Posts:
    1
    How do i stop the score from incrementing after i collide with and enemy?!


    using UnityEngine;
    using System.Collections;
    public class PointCounter : MonoBehaviour {
    public int score;


    // Use this for initialization
    void Start () {
    score = 0;

    }
    // Update is called once per frame
    void FixedUpdate() {
    score++;
    }

    //checks for entering a trigger
    void OnTriggerEnter(Collider other){
    //checks other collider's tag
    if(other.gameObject.tag == "Enemy"){
    score = score--;

    }


    }

    }
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Hi,
    A few points here: :)

    Please use code tags when posting code (check the tool bar just above the edit panel when you are typing).

    With this code:
    Code (CSharp):
    1. // Update is called once per frame
    2. void FixedUpdate() {
    3.     score++;
    4. }
    1. Update is called once per frame (as is LateUpdate).
    2. FixedUpdate may, or may not, be called once per frame.
    3. You are incrementing your score at fixed time intervals. This has nothing to do with trigger events.
     
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    here is what you need. a bool to set and check against.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PointCounter : MonoBehaviour
    4. {
    5.     public int score;
    6.     private bool hitEnemy = false;
    7.  
    8.  
    9.  
    10.     void Start ()
    11.     {
    12.     score = 0;
    13.  
    14.     }
    15.  
    16.     //FixedUpdate is only used for physics stuff
    17.     void FixedUpdate() {
    18.  
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if(!hitEnemy)
    24.         {
    25.             score++;
    26.         }
    27.    
    28.     }
    29.  
    30.     //checks for entering a trigger
    31.     void OnTriggerEnter(Collider other)
    32.     {
    33.         //checks other collider's tag
    34.         if(other.gameObject.tag == "Enemy")
    35.         {
    36.             hitEnemy = true;
    37.             score = score--;
    38.         }
    39.     }
    40.  
    41. }
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Hi johne5,

    That is fine in so far as it goes, but would it not need a
    hitEnemy = false
    in an
    OnTriggerExit
    to stop scoring at some point? Either that or the disabling of that script at some later stage.
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    @Doug_B In Update i'm checking for if hitEnemy == false,
    then in the OnTriggerEnter i'm setting the bool to true, that is how i'm stopping the increase of points to the score.

    in OnTriggerExit, he could set it back to false if he wants to start adding to the score again
     
  6. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    updated the code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PointCounter : MonoBehaviour
    4. {
    5.     public int score;
    6.     private bool hitByEnemy = false;
    7.     void Start ()
    8.     {
    9.         score = 0;
    10.     }
    11.     //FixedUpdate is only used for physics stuff
    12.     void FixedUpdate() {
    13.     }
    14.     void Update()
    15.     {
    16.         if(!hitByEnemy)
    17.         {
    18.             score++;
    19.         }else
    20.         {
    21.             score--;
    22.         }
    23.  
    24.     }
    25.     //checks for entering a trigger
    26.     void OnTriggerEnter(Collider other)
    27.     {
    28.         //checks other collider's tag
    29.         if(other.gameObject.tag == "Enemy")
    30.         {
    31.             hitByEnemy = true;
    32.         }
    33.     }
    34.    
    35.     void OnTriggerExit(Collider other)
    36.     {
    37.         //checks other collider's tag
    38.         if(other.gameObject.tag == "Enemy")
    39.         {
    40.             hitByEnemy = false;
    41.         }
    42.     }
    43. }
     
  7. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, I see that the score is going to change, one way or another, every frame there. The original requirement was to 'stop incrementing'. Guess we'll have to wait to hear from the OP to see if that is what they wanted. :)