简体   繁体   English

如何统一引用来自不同脚本的 function 和变量?

[英]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.我正在 Unity 中制作一个游戏,你是一个白色箭头,始终指向鼠标,你可以向寻路的敌人射击以摧毁他们。 If 5 pathfinding enemies hit you, the game restarts.如果 5 个寻路敌人击中你,游戏将重新开始。 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.但是有一个漏洞 - 得分每秒增加 1 倍,每 15 次射击就会产生敌人,所以我决定每次用子弹击中敌人时使得分 go 增加 1。 To do this, I created a score script:为此,我创建了一个score脚本:

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:enemy的脚本中,我告诉它在被击中后摧毁自己之前将分数加 1:

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.但是当我运行游戏时,没有错误,但是分数没有 go 上升。 I played around for a bit and found out that the score script doesn't receive the command to increase the score.我玩了一会儿,发现score脚本没有收到增加分数的命令。 Please help!请帮忙!

Thanks谢谢

Are you sure your script goes into your "if(counter % 15 == 0)" condition?您确定您的脚本进入您的“if(counter % 15 == 0)”条件吗? Make sure to put some "Debug.Logs()" in those situations to know what is happening in your code.确保在这些情况下放置一些“Debug.Logs()”以了解代码中发生了什么。 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.在您当前的代码中,我可以告诉您,您的分数只会在计数器变为 15 时才会增加,并且不会变为 15,因为您总是在“if(counter % 15 == 0)”条件下用 1 次命中来摧毁敌人.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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