0

I'm making a game in Unity where you're a white arrow, pointing to the mouse at all times, and you can shoot at pathfinding enemies to destroy them. If 5 pathfinding enemies hit you, the game restarts. But there's an exploit - the score goes up 1x per second and enemies spawn in every 15 shots, so I decided to make the score go up by 1 every time you hit an enemy with a bullet. To do this, I created a score script:

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

public class score : MonoBehaviour
{
    public float scoreCount = 0f;
    public Text scoreText;
    private bool b = true;

    public void AddToScore()
    {
        scoreCount += 1f;
        Debug.Log("Added 1 to score");
    }

    void Update()
    {
        scoreText.text = scoreCount.ToString("0");
    }

and in the enemy script I told it to add 1 to the score before destroying itself after being hit:

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

public class enemy : MonoBehaviour
{
    public float counter = 0f;
    public float spawnRadius = 10f;
    public GameObject enemyGO;
    public float scoreCounter = 0f;


    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "bullet")
        {
            counter += 1f;
            if (counter % 15 == 0)
            {
                Vector2 spawnPos = GameObject.Find("Player").transform.position;
                spawnPos += Random.insideUnitCircle.normalized * spawnRadius;
                enemyGO = Instantiate(gameObject, spawnPos, Quaternion.identity);
                enemyGO.GetComponent<Pathfinding.AIDestinationSetter>().target = GameObject.Find("Player").transform;

                GameObject.Find("Player").GetComponent<score>().AddToScore(); // HERE'S THE SCORE ADDING BIT

                
            }
            Destroy(gameObject, 0.1f);


            // GameObject player = GameObject.Find("Player");
            // scoreCounter = scoreCounter + 1f;
            // player.GetComponent<score>().scoreCount = scoreCounter;

        }
        if (collision.gameObject.tag == "player")
        {
            Destroy(gameObject, 0.1f);
        }
    }
}

But when I run the game, there are no errors, but the score doesn't go up. I played around for a bit and found out that the score script doesn't receive the command to increase the score. Please help!

Thanks

4
  • 1
    why use float for counter Commented Nov 27, 2020 at 18:31
  • @Ruzihm when I first started making the game I was using the counter for something else and haven't changed it since - but it doesn't affect the game so it's fine Commented Nov 27, 2020 at 18:33
  • 1
    as @Çağatay IŞIK pointed out below, you destroy the enemy before your counter can increase to 15. Try moving your counter to the player instead of being an instance field on enemy Commented Nov 27, 2020 at 18:34
  • @Ruzihm thanks very much - I'm off to change it now!! Commented Nov 27, 2020 at 18:36

1 Answer 1

1

Are you sure your script goes into your "if(counter % 15 == 0)" condition ? Make sure to put some "Debug.Logs()" in those situations to know what is happening in your code. In your current code i can tell that your score will only increase when the counter becomes 15 and it won't become 15 since you are always destroying your enemies with 1 hit out of your "if(counter % 15 == 0)" condition.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much! I'll do that now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.