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

Need some assistance with my scoring C#

Discussion in 'Scripting' started by Frankzillaman, Oct 15, 2015.

  1. Frankzillaman

    Frankzillaman

    Joined:
    Mar 20, 2015
    Posts:
    22
    So i have a trigger attached to my prefabs for the player to score when they jump over , or go under the obstacle correctly. But, there is 2 problems i am running into.

    1. Player still scores when they collide with the obstacle, when it shouldn't add to the score.

    2. It is suppose to multiply score after a few completed obstacles in a row, but is not working.

    Any help would be appreciated, i am probably over looking something simple.


    Code (CSharp):
    1. public class obstaclePointTrigger : MonoBehaviour {
    2.     private mainRoot pMainRoot = null;
    3.     private gameRoot pGameRoot = null;
    4.     public static int playerScore = 0;
    5.     public int pScore = 10;
    6.     public int combo = 3;
    7.     public int multiplier = 2;
    8.  
    9.     private void Start () {
    10.         findObjects();
    11.     }
    12.     //
    13.     private void findObjects(){
    14.         pMainRoot = GameObject.Find ("mainRoot").GetComponent<mainRoot> ();
    15.         pGameRoot = GameObject.Find ("gameRoot").GetComponent<gameRoot> ();
    16.     }
    17.     //
    18.     private void OnTriggerEnter (Collider other) {
    19.         if (combo >= 3) {
    20.             multiplier = 2; //the increased points
    21.         } else {
    22.             multiplier = 1;
    23.         }
    24.         //
    25.         if (gameObject.CompareTag ("Pickup")) {
    26.             gameObject.SetActive (false);
    27.             pGameRoot.changeScore(pScore);
    28.             //Debug.Log ("Points triggered");
    29.         }
    30.         if (gameObject.CompareTag ("obstacle")) {
    31.             //pGameRoot.changeScore(pScore * -1);
    32.         }
    33.     }
    34. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    In your OnTriggerEnter you are checking gameObject.CompareTag

    Don't you wanna check the game object associated with the "other" collider?

    I'm thinking that would be other.gameObject...

    If that's not what you mean, then put in a Debug.Log( name + " --- " + tag); at around line 25 and see who you're hitting.

    Also, I never see you incrementing combo. It is set to 3 by default but could be changed to something else in the editor.
     
  3. Frankzillaman

    Frankzillaman

    Joined:
    Mar 20, 2015
    Posts:
    22
    Thx for some tips @Kurt Dekker i have a question. I was thinking of disabling scoring for a second or 2 if player collides with an obstacle to help with the scoring issue i am having where the player is still scoring even if they hit the obstacle. Im guessing i would disable my point trigger with a waitforseconds code, is this correct? if so, whats the easiest way to go about that? Never really used the waitforseconds function.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    You would probably want to use something like a coroutine, and essentially you would hand that coroutine a reference to the thing you wanted to disable, and the length of time you want it disabled for, and then at the end of the coroutine, it would turn it back on.

    Take a look at how coroutines work. They can be a bit tricky to wrap your head around but they are exactly what you want.

    Here might be an example to turn off a collider momentarily:

    Code (csharp):
    1.     IEnumerator DisableForTime( Collider collider, float time)
    2.     {
    3.         collider.enabled = false;
    4.         yield return new WaitForSeconds( time);
    5.         collider.enabled = true;
    6.     }
    7.  
    8.     // and then if you had the collider already stored as "other,"
    9.     // you would do this to disable it for 2 seconds:
    10.  
    11.     StartCoroutine( DisableForTime( other, 2.0f)];