简体   繁体   中英

How to reference a function and variable from a different script in unity?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM