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

My gemScript add 2 scoreValue and I want only one

Discussion in '2D' started by monsieurmilien, Apr 12, 2020.

  1. monsieurmilien

    monsieurmilien

    Joined:
    Mar 29, 2020
    Posts:
    10
    Heya guys, I work on a game's project and I have a problem I want to collect gem but my script have a problem sometimes when I collect gem the script add 2 to scoreValue and sometimes add 1. I only want when i collect gem that add 1 to the scoreValue. Can you help me.
    Here it's my scripts.

    My GemScript :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GemScript : MonoBehaviour
    6. {
    7.     void OnTriggerEnter2D(Collider2D col)
    8.     {
    9.         ScoreScript.scoreValue += 1;
    10.         Destroy(gameObject);
    11.     }
    12. }
    13.  
    My ScoreScript :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ScoreScript : MonoBehaviour
    7. {
    8.     public static int scoreValue = 0;
    9.     Text score;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         score = GetComponent<Text>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         score.text = "SCORE : " + scoreValue;
    20.     }
    21. }
    22.  
    Thanks very much
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @monsieurmilien

    I find it bit hard to believe that OnTriggerEnter2D would somehow run twice, I haven't seen such thing to happen.

    Make sure you don't have overlapping gem objects and you could also try adding a "gem collected" bool into gem, and only add score if gem is not yet collected.

    Also, you don't do any kind of checks in your OnTriggerEnter2D which object collided with it. You most likely want to only register contacts with player character?
     
    monsieurmilien likes this.
  3. monsieurmilien

    monsieurmilien

    Joined:
    Mar 29, 2020
    Posts:
    10
    Hi @eses

    It what I want, I want to when my player collide with the gem, the gem disappear and add to the score +1.

    I verified, there no gem in double.

    I don't understand what you want to say about a "gem collected" bool. can you tell me more please.

    Thanks very much for your help.
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @monsieurmilien

    "I don't understand what you want to say about a "gem collected" bool."

    Exactly what I tried to explain... typical boolean check to make sure you don't do the collecting twice.
    Code (CSharp):
    1. public class GemScript : MonoBehaviour
    2. {
    3.    public bool collected;
    4.  
    5.     void OnTriggerEnter2D(Collider2D col)
    6.     {
    7.         if (collected)
    8.              return;
    9.  
    10.         ScoreScript.scoreValue += 1;
    11.         collected = true;
    12.         Destroy(gameObject);
    13.     }
    14. }
     
  5. monsieurmilien

    monsieurmilien

    Joined:
    Mar 29, 2020
    Posts:
    10
    Oh thanks now I undersand. Now I can't developp my OWN GAME
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    "Oh thanks now I undersand. Now I can't developp my OWN GAME"

    Why can't you? Or did you accidentally write incorrectly. Anyway, let me know if there is still an issue.
     
    monsieurmilien likes this.
  7. monsieurmilien

    monsieurmilien

    Joined:
    Mar 29, 2020
    Posts:
    10
    Oh no my corrector, Now I CAN developp MY OWN GAME***
     
    eses likes this.
  8. monsieurmilien

    monsieurmilien

    Joined:
    Mar 29, 2020
    Posts:
    10
    And sorry but I have another problem, there is few person on the internet who have the same problem but no responses. So I do my PauseMenu but the UI button don't work (they work before I add my PauseMenu into my prefabs folder)
    Can you know how to fix that ??

    Thanks a lot again :)
     
  9. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @monsieurmilien

    I'd ask another question, as it is different topic.

    I bet it might be some screen covering UI element in your pause menu - as first UI element that gets a hit, will stop the clicks... so think it like transparent glass over your button preventing a button press.

    However, if it is something else, then ask another question, and provide necessary code, otherwise one can just guess.
     
  10. monsieurmilien

    monsieurmilien

    Joined:
    Mar 29, 2020
    Posts:
    10
    Okay, thanks I'll see that now, Bye and thanks again

    (BTW I'm french so sorry for my approximative english)
     
  11. monsieurmilien

    monsieurmilien

    Joined:
    Mar 29, 2020
    Posts:
    10
    @eses It was that thanks very much you release me from a weight
     
    eses likes this.