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

Score is incrementing by 4 instead of 1

Discussion in '2D' started by manan966, Oct 19, 2020.

  1. manan966

    manan966

    Joined:
    Sep 7, 2020
    Posts:
    57
    For this I need A Proper Score Manager, every time i dodge the blocks score should be updated but for some reason its not happening
    Screenshot 2020-10-19 092532.png

    and this is how i caclculate my score
    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.     }
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Don't think there is enough info here to know for sure, but one thing that looks suspicious to me is your image shows 4 cloned objects. If your score is likewise incrementing by 4 instead of 1, a good suspect is that all 4 clones are firing the increment logic. Why that would be, I can't tell from this, but it is a good place to start looking.
     
  3. manan966

    manan966

    Joined:
    Sep 7, 2020
    Posts:
    57
    I guessed it but dont know exactly how to solve it and what more info do you need tell me...


    This is my Blockspawner code
    Code (CSharp):
    1. public class blockspawner : MonoBehaviour
    2. {
    3.     public Transform[] spawnPoints;
    4.     public GameObject blockPrefab;
    5.     private float timeToSpawn = 2f;
    6.     public float interval = 1f;
    7.  
    8.     void Update()
    9.     {
    10.         if (Time.time >= timeToSpawn)
    11.         {
    12.             SpawnBlocks();
    13.             timeToSpawn = Time.time + interval;
    14.          
    15.  
    16.         }
    17.     }
    18.     void SpawnBlocks()
    19.     {
    20.         int randomIndex = Random.Range(0, spawnPoints.Length);
    21.         for (int i = 0; i < spawnPoints.Length; i++)
    22.         {
    23.             if (randomIndex != i)
    24.             {
    25.                 Instantiate(blockPrefab, spawnPoints[i].position, Quaternion.identity);
    26.             }
    27.         }
    28.     }
    29. }
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    "and this is how i caclculate my score"

    Could you be a little bit more verbose? You didn't event mention to what object you have attached that script to... no image of inspector either...
     
  5. manan966

    manan966

    Joined:
    Sep 7, 2020
    Posts:
    57
    ohh ok Thank you..... so that score script is attached to the blocks prefab name of the script is "destroy"
    and here is the inspector window of one of the clones
    Screenshot (14).png

    That destroy script is on all 4 blocks...it may be the reason score is incrementing by 4 but its just a guess i dont know
    actual reason.
     
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,453
    You should show the ScoreInstance script with the GetScore function you mentioned in your original post.
     
  7. manan966

    manan966

    Joined:
    Sep 7, 2020
    Posts:
    57
    ohh ok here it is
    Code (CSharp):
    1. public class Scorein : MonoBehaviour
    2. {
    3.     public static Scorein instance;
    4.     public Text scoreText;
    5.     private int scr;
    6.  
    7.     void Start()
    8.     {
    9.  
    10.  
    11.         PlayerPrefs.SetInt("Score", Scorein.instance.GetScore());
    12.     }
    13.     // Start is called before the first frame update
    14.     void Awake()
    15.     {
    16.         scoreText = GameObject.Find("scoresin").GetComponent<Text>();
    17.         MakeInstance();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void MakeInstance()
    22.     {
    23.         if (instance == null)
    24.             instance = this;
    25.     }
    26.     public void IncrementScore()
    27.     {
    28.         scr++;
    29.         scoreText.text = "Score:" + scr;
    30.  
    31.     }
    32.     public int GetScore()
    33.     {
    34.         return this.scr;
    35.     }
    36. }