Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Stop Clones from firing Score Increment logic

Discussion in 'Getting Started' started by manan966, Oct 27, 2020.

  1. manan966

    manan966

    Joined:
    Sep 7, 2020
    Posts:
    57
    All four blocks will fire the score increment logic which increases the score by 4 instead of 1

    this inspector in which blocks are destroyed and score is counted

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (transform.position.y < -2f && Scorein.instance != null)
    4.         {
    5.             Scorein.instance.IncrementScore();
    6.             PlayerPrefs.SetInt("Score", Scorein.instance.GetScore());
    7.         }
    8.         else if (Scorein.instance == null)
    9.         {
    10.             PlayerPrefs.SetInt("Score", Scorein.instance.GetScore());
    11.         }
    12.         if (transform.position.y < -2f)
    13.         {
    14.             Destroy(gameObject);
    15.         }
    16.     }
    now this is Score script in which get score is there
    Code (CSharp):
    1. public static Scorein instance;
    2.     public Text scoreText;
    3.     private int scr;
    4.     void Start()
    5.     {
    6.         PlayerPrefs.SetInt("Score", Scorein.instance.GetScore());
    7.     }
    8.     // Start is called before the first frame update
    9.     void Awake()
    10.     {
    11.         scoreText = GameObject.Find("scoresin").GetComponent<Text>();
    12.         MakeInstance();
    13.     }
    14.     // Update is called once per frame
    15.     void MakeInstance()
    16.     {
    17.         if (instance == null)
    18.             instance = this;
    19.     }
    20.     public void IncrementScore()
    21.     {
    22.         scr++;
    23.         scoreText.text = "Score:" + scr;
    24.     }
    25.     public int GetScore()
    26.     {
    27.         return this.scr;
    28.     }
    I am beginner in this aspect and am very Confused ,Please need someone to help.