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. Dismiss Notice

Why my score disappeared and refresh to 0 along with the destroy of prefab gopher?

Discussion in 'Scripting' started by king_king_king, Apr 29, 2021.

  1. king_king_king

    king_king_king

    Joined:
    Apr 27, 2021
    Posts:
    3
    Hi,im new to unity for few weeks and trying to get my first 2D game work through.It's just a Whac-A-Mole type game but i cant get the score board working correctly. I want to count and show how many gophers has been beaten but the number just refresh to 0 as the gopher was destroyed. I want a 0-1-2-3-4-5..... type but it's just 0-1-0-1. The scripts are as bellow,help please!

    public class fen : MonoBehaviour
    {
    // Start is called before the first frame update
    private int count;
    public Text countText;
    public GameObject m_Prefab2;
    void SetCountText()
    {
    countText.text = "" + count.ToString();
    }
    void Start()
    {
    count = 0;
    SetCountText();
    }
    void OnMouseDown()
    {
    Instantiate(m_Prefab2, transform.position, Quaternion.identity);
    gameObject.SetActive(false);
    //Destroy(gameObject,10000f);
    count += 1;
    SetCountText();
    }
    }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    I'm guessing this script is on the gophers? So, let's walk through the logic.

    So, let's say the first gopher turns on and runs it's Start. It's Start will set countText.text to 0 because that's what you have it doing.

    Now you bop the gopher and you increment it's count variable and set countText to 1.

    Now gopher 2 is activated and it runs Start, which sets countText back to 0. Then you bop that gopher which increments it's count variable to 1 and sets the countText to 1.

    This will continue unless you bop the same gopher which will increment it's value by 1 again.

    Remember that each gopher has it's own copy of the count variable. What you want instead is a manager class that maintains a single count variable with a reference to countText. Then when you bop a gopher, call a method to increment the count and assign the value to countText.
     
    king_king_king likes this.
  3. king_king_king

    king_king_king

    Joined:
    Apr 27, 2021
    Posts:
    3
    Thank you very much!

    Here are the changes I made with the help of your advice, I made an UIManager but it didn't work, so I'm wondering if their's any example to tofollow?
    屏幕截图 2021-04-30 163319.png 屏幕截图 2021-04-30 163343.png
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    In your UIManager class, you need to make your UIManager _instance static.

    Then in your OnMouseDown you can see the red underline which tells you there is an error there.

    You would call it with
    UIManager._instance.Addscore();

    Also, I suggest you take your line of code in Update and just move it under your score+= 1; code. Then it updates the text right after Updating the score value. No reason to do this in Update.

    Finally, post code in the forums with code tags. It's just much nicer to help you that way.
     
  5. king_king_king

    king_king_king

    Joined:
    Apr 27, 2021
    Posts:
    3
    I MADE IT ! So MUCH appreciate !

    I posted code with code tag yesterday,however it kept reported error and rejected upload. I will pay more attention in the future.

    Again, thank you for helping me! You saved my game, thanks a lot !
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    No problem. Takes time to learn these things.