【问题标题】:Unity2D: Why won't a variable from a different script update in this script?Unity2D:为什么来自不同脚本的变量不会在此脚本中更新?
【发布时间】:2020-07-02 11:09:56
【问题描述】:

这是第一个仅用于“收集”宝石的脚本。

using UnityEngine;
public class GemDestructionScript : MonoBehaviour
{
public int gemsCollected = 0;

void Awake()
{
}

void Update()
{
}

void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag.Equals("Player1"))
{
gemsCollected++;
GetComponent<SpriteRenderer>().enabled = false;
GetComponent<CircleCollider2D>().enabled = false;
}
}
}

这是第二个脚本,用于将gemsCollected 显示在画布上。

using UnityEngine;
using UnityEngine.UI;
using System;
public class GemCounter : GemDestructionScript
{
private Canvas canvas;

void Start()
{
}

void Update()
{
Transform child = transform.Find("Text");
Text t = child.GetComponent<Text>();
t.text = Convert.ToString(gemsCollected);
}
}

问题是gemsCollected 变量不会在画布上更新,它一直保持在 0。 提前致谢。

【问题讨论】:

    标签: c# unity3d variables


    【解决方案1】:

    我看不到 GemCounter 需要从 GemDestruction 继承。当不同的类别相互依赖时,继承更常用。

    Unity 的 Fruit 示例就是一个很好的简单示例

    在您的情况下,我建议使用 2 个不通过继承链接的单独类。这将使您免去正确访问所需数据的麻烦,除非使用继承对您来说非常重要。

    当您拆分班级并将gemsCollected 移至柜台时,而不是在宝石本身上,您可以让宝石在收集时“报告”。这样,计数器可以跟踪收集了多少宝石并将它们显示在 UI 上。每个对象都应该有自己的工作,并且只使用与该工作相关的东西。

    例子:

    public class GemDestructionScript : MonoBehaviour
    {
        GemCounter counter;
    
        private void Start()
        {
            // Find where-ever you keep the GemCounter Script (the Canvas in your case)
            counter = GameObject.Find("Canvas").GetComponent<GemCounter>();
        }
    
        void OnCollisionEnter2D(Collision2D col)
        {
            if (col.gameObject.tag.Equals("Player1"))
            {
                counter.AddGem();
                GetComponent<SpriteRenderer>().enabled = false;
                GetComponent<CircleCollider2D>().enabled = false;
            }
        }
    }
    
    public class GemCounter : MonoBehaviour
    {
        public int gemsCollected = 0;
    
        Text GemCounterText;
    
        private void Start()
        {
            // Finding the Text Object in every Frame would be inefficient, so unless that object is constantly changing, just get it once during Start/Awake/before using it
            GemCounterText = GameObject.Find("Text").GetComponent<Text>();
        }
    
        public void UpdateGemCount()
        {
            GemCounterText.text = gemsCollected.ToString();
        }
    
        public void AddGem()
        {
            gemsCollected++;
            UpdateGemCount();
        }
    }
    

    【讨论】:

    • 由于某种原因,现在它根本不会显示,即使是零也没有显示任何错误,而且我认为每次宝石被摧毁时我都没有完全解释我的自我屏幕右上角至少得到 +1,这就是它的工作方式。
    • 根据您的 cmets,根据您的新信息编辑了我的答案代码。你拥有的每一个对象都应该有一个工作,它只做那个工作。您的计数器保持计数并显示收集的宝石总数。您的宝石会记录它们已被收集并告诉柜台它们已被收集,然后消失。您不想要/不需要的是每颗宝石(即使它们已经被收集和消失)来计算已收集的数量。如果您碰巧有 100 颗宝石,那么它们都持有与其在场景中的功能无关的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 2012-12-26
    • 2013-03-15
    • 2011-09-20
    相关资源
    最近更新 更多