Home>

I am making a process to increase the score by +1 when hitting a bullet.
For that purpose, I wrote a code to display the score (upper side) and a code to increase the score by +1 when the bullet hits (lower side).

  public class ScoreCounter: MonoBehaviourPunCallbacks
    {
        Text Score Text;
        int score;
        public int Score
        {
            set
            {
                ScoreUpdate ();
                score = value;
            }
            get
            {
                return score;
            }
        }
        void Start ()
        {
            ScoreText = GetComponent<Text>();
            Score = 0;
        }
        void ScoreUpdate ()
        {
            ScoreText.text = "Score:" + Score.ToString ("00");
            Debug.Log (Score);
        }
    }
  void OnTriggerEnter (Collider other)
    {
        if (other.gameObject.tag == ("Player"))
        {
            scoreCounter.Score + = 1;
            Debug.Log (scoreCounter.Score);
        }
    }


The code for displaying the score is attached to the UI that actually displays the score.
The code that adds +1 to the score when the bullet hits is attached to the bullet.

But these two codes didn't work.
Specifically, the value of the Score variable that has been incremented by +1 on the bullet side and the value of the Score variable on the ScoreCounter side that actually displays the score are out of alignment.
For example, if the value of the Score variable on the bullet side is 1, the value on the Score Counter side is 0, if the value of the Score variable on the bullet side is 2, the value on the Score Counter side is 1, and so on. I will end up.

I don't know what caused this to happen.
Is there a problem with the two codes in this question?
Or is it likely that something else is wrong?
If i have problems with these two codes, please tell me which part is the problem.

  • Answer # 1

    set
    {
        ScoreUpdate ();
        score = value;
    }


    I think it's because of the processing order of this part.
    ScoreCounterofScore propertyIf set to, the value will bevalueIt is handed over atScoreUpdateSince being calledscoreA new value has been assigned to.

  • Answer # 2

    set
    {
    ScoreUpdate ();
    score = value;
    }

    These two lines are upside down, aren't they?