Do something every thousand score

i am making a top down shooter game and i want a upgrade system to buy upgrades you would need upgradepoints i want the player to get them every 1000 score so i would want a script that does it every 1000, 2000, 3000, 4000 … i found something but it did not work please help here is my script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class movement : MonoBehaviour {

    //Rotate

    private Vector3 mouse_pos;
    public Transform target;
    private Vector3 object_pos;
    private float angle;

    //Shooting

    public float Shootspeed = 3;
    private float _shootspeed;
    public GameObject Bullet;

    //Die
    public GameObject DeadScreen;

    //upgrades

    public float upgradepoints;

    public Text pointstext;

    //multishot
    public float multishotlvl = 0f;


    void Update () {
        //Rotate
        mouse_pos = Input.mousePosition;
        mouse_pos.z = -20;
        object_pos = Camera.main.WorldToScreenPoint(target.position);
        mouse_pos.x = mouse_pos.x - object_pos.x;
        mouse_pos.y = mouse_pos.y - object_pos.y;
        angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0, 0, angle);

        //Shoot

        if (Input.GetMouseButton(0))
        {
            _shootspeed = _shootspeed - Time.deltaTime;
            if (_shootspeed <= 0)
            {
                Instantiate(Bullet, transform.position, transform.rotation);
                _shootspeed = Shootspeed;
                //Multishot upgrades \/
                if (multishotlvl >= 1)
                {
                    GameObject projectile2 = Instantiate(Bullet, transform.position, transform.rotation);
                    projectile2.transform.Rotate(transform.rotation.x, transform.rotation.y, transform.rotation.z - 18f);
                }
                if (multishotlvl >= 2)
                {
                    GameObject projectile3 = Instantiate(Bullet, transform.position, transform.rotation);
                    projectile3.transform.Rotate(transform.rotation.x, transform.rotation.y, transform.rotation.z + 18f);
                }
            }
        }

        //upgrades

        int tmp = (int)Scores.Score % 200;
        if (tmp == 0 || tmp == 100)
        {
            Debug.Log("addpoints");
        }

        if (multishotlvl >= 3)
        {
            Debug.Log("Alreadymaxed");
        }
        else
        {
            if (Input.GetKey(KeyCode.Alpha1))
            {
                if (upgradepoints >= 1)
                {
                    upgradepoints = upgradepoints - 1;
                    multishotlvl = multishotlvl + 1;
                }
            }
        }
    }

    //Die if hit thing tagged enemy
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            Debug.Log("Player Died By " + collision.gameObject.name);
            Die();
        }
    }
    public void Die()
    {
        DeadScreen.SetActive(true);
        Vector3 deadpos = transform.position;
        deadpos.x = 9999;
        transform.Translate(deadpos);
    }
}

Here is the problem


        int tmp = (int)Scores.Score % 200;
        if (tmp == 0 || tmp == 100)
        {
            Debug.Log("addpoints");
        }

        if (multishotlvl >= 3)
        {
            Debug.Log("Alreadymaxed");
        }
        else
        {
            if (Input.GetKey(KeyCode.Alpha1))
            {
                if (upgradepoints >= 1)
                {
                    upgradepoints = upgradepoints - 1;
                    multishotlvl = multishotlvl + 1;
                }
            }
        }
    }

I found a solution myself
for anybody else who searches this here is what i did:

        float Bonusscore = 1000f;

        if (Scores.Score >= Bonusscore)
        {
            upgradepoints += 1f;
            Bonusscore = Bonusscore + 1000f; ;
        }

Glad you were able to find a solution :slight_smile:
I see you tried to use the % operator in the code you had issues with.

Here it will store the reminder of Score / 200 in the tmp variable
int tmp = (int)Scores.Score % 200;

Here you check if the reminder is 0 or 100, so in this case because you’re checking if the reminder is 100 or 0, then the if condition is true every 100th score. If you were to remove the || tmp == 100 from the if statement, then it would be true every 200th.

if (tmp == 0 || tmp == 100)
{
}

To make this to work with every 1000th you would have to do:
if(Scores.Score % 1000 == 0)

All it does is checking to see if there’s no reminders.
If using this method you also have to check if the score is more than 0 or else it would be true when the score is 0, because 0 / 1000 has 0 reminders :slight_smile:

Hope that helps with why the previous code didn’t work :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms