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--; } } }
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): // Update is called once per frame void FixedUpdate() { score++; } Update is called once per frame (as is LateUpdate). FixedUpdate may, or may not, be called once per frame. You are incrementing your score at fixed time intervals. This has nothing to do with trigger events.
here is what you need. a bool to set and check against. Code (CSharp): using UnityEngine; using System.Collections; public class PointCounter : MonoBehaviour { public int score; private bool hitEnemy = false; void Start () { score = 0; } //FixedUpdate is only used for physics stuff void FixedUpdate() { } void Update() { if(!hitEnemy) { score++; } } //checks for entering a trigger void OnTriggerEnter(Collider other) { //checks other collider's tag if(other.gameObject.tag == "Enemy") { hitEnemy = true; score = score--; } } }
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.
@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
updated the code Code (CSharp): using UnityEngine; using System.Collections; public class PointCounter : MonoBehaviour { public int score; private bool hitByEnemy = false; void Start () { score = 0; } //FixedUpdate is only used for physics stuff void FixedUpdate() { } void Update() { if(!hitByEnemy) { score++; }else { score--; } } //checks for entering a trigger void OnTriggerEnter(Collider other) { //checks other collider's tag if(other.gameObject.tag == "Enemy") { hitByEnemy = true; } } void OnTriggerExit(Collider other) { //checks other collider's tag if(other.gameObject.tag == "Enemy") { hitByEnemy = false; } } }
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.