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

Going to get a tumour from this score system xD

Discussion in '2D' started by TurduckenMan, Apr 26, 2014.

  1. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    So I think I made a big mistake with how I did this somewhere, but through a couple days of toying around and trying to get my score to work properly, I've finally gotten the scoreboxes to loop back with the obstacles in my game perfectly. The problem now is that when I hit a scorebox, rather than scoring a single point I'm gaining around 9 each time I go through one. I thought my code had an answer for this, but it doesn't seem to be working. What I'm thinking is that because I have three different score related scripts throughout my game, maybe there's some interference or something but I can't for the life of me discern what could be causing it.

    My first script is in the script for my character:

    Code (csharp):
    1.  bool ScoreActive = true;
    2.  
    3. void OnTriggerEnter2D(Collider2D collider)  {
    4.        
    5.         if (godMode)
    6.            
    7.             return;
    8.        
    9.         if (collider.tag == "ScoreBox") {
    10.            
    11.             if (ScoreActive == true) {
    12.                
    13.                 Score.AddPoint ();
    14.                
    15.                 ScoreActive = false;
    16.                
    17.             }
    18.            
    19.         } else {
    20.  
    21.             animator.SetTrigger ("Dead");
    22.             dead = true;
    23.            
    24.         }
    25.        
    26.     }
    27. }
    The second code is my score script which I have set into a GUIText, obviously displaying the score to the player:

    Code (csharp):
    1.     static int score = 0;
    2.     static int highScore = 0;
    3.  
    4.     static public void AddPoint() {
    5.                 score++;
    6.  
    7.                 if(score > highScore) {
    8.                         highScore = score;
    9.                 }
    10.         }
    11.  
    12.     void Start(){
    13.         score = 0;
    14.                 highScore = PlayerPrefs.GetInt ("highScore", 0);
    15.         }
    16.  
    17.     void OnDestroy() {
    18.                 PlayerPrefs.SetInt ("highScore", highScore);
    19.         }
    20.     // Update is called once per frame
    21.     void Update () {
    22.         guiText.text = "Score: " + score + "\nHigh Score: " + highScore;
    23.     }
    24. }
    And finally I have another score script in the scorebox itself, which is where I think the problem may be coming from:

    Code (csharp):
    1.     void OnTriggerEnter2D(Collider2D collider) {
    2.         if(collider.tag == "Player") {
    3.             Score.AddPoint();
    4.             }
    5.         }
    6.     }
    I've attempted to disable the last code and the problem still persisted, so I'm planning on getting rid of it but I've put it off just incase you guys think I should keep it or something. The code in my player is needed as it is what distinguishes that the score box isn't a normal obstacle and prevents the player from dying when hitting hit.

    Any help would be much appreciated!
     
    Last edited: Apr 26, 2014
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Problem is more likely in your movement code than your trigger code; off-hand, I'd guess you were moving colliders in Update instead of FixedUpdate.
     
  3. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    I thought it might've been to at first, but the looping is being done by a looper which is following behind the player, and the code for the looper itself actually uses a OnEnter2D rather than update/fixedupdate.
     
  4. TurduckenMan

    TurduckenMan

    Joined:
    Apr 20, 2014
    Posts:
    29
    Okay so I tried removing all mention of the score from my player and just leaving it as "if he's hitting the scorebox, don't kill him" Then I moved my score code to the other code which now looks like this:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ScorePoint : MonoBehaviour {
    5.  
    6.     bool ScoreActive = true;
    7.  
    8.     void OnTriggerEnter2D(Collider2D collider) {
    9.         if(collider.tag == "Player") {
    10.             if(ScoreActive == true){
    11.             Score.AddPoint();
    12.                 ScoreActive = false;
    13.             }
    14.             }
    15.         }
    16.  
    17.     void OnTriggerExit2D(Collider2D collider) {
    18.                 ScoreActive = true;
    19.     }
    20. }
    And it's actually working much better, minus a few cases.
    After scoring the first six points (the number of points possible before the boxes have all been hit at least once), the bool is only SOMETIMES being set back to true. Most times, it works, but about 30% of the time, no score is added. Everything else is working just fine, but I need a 100% consistency with the variable being toggled back to true.
    Thanks for any help!

    [EDIT] Nevermind guys I got it all figured out. Thanks for the help though! Sorry for posting so much :p
     
    Last edited: Apr 26, 2014