不知道为什么用“this”来检查

Do not know why using "this" for checking

提问人:Chí Bảo Nguyễn 提问时间:12/23/2022 更新时间:12/23/2022 访问量:55

问:

我创建了一个名为 包含场景中所有游戏对象的数组。我还创建了 foreach 循环来检查是否附加了 .我正在关注这本书,但它没有清楚地解释为什么我们需要.ZombiesZombiesZombieState.cszState==this

using UnityEngine;

public class ZombieState : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        GameObject[] Zombies = (GameObject[])GameObject.FindObjectsOfType(typeof(GameObject));
        foreach (GameObject go in Zombies)
        {
            ZombieState zState = go.GetComponent<ZombieState>();
            if (zState == null || zState == this)
            {
                continue;
            }
        }

    }
}

c# unity-game-engine 这个

评论

0赞 Enigo 12/24/2022
关键字引用类的当前实例。在您的情况下,请检查是否与游戏对象的当前实例相同。你的代码看起来不完整,所以在不知道你想用什么找到的情况下,很难说这里的正确处理方式应该是什么。但看起来你应该是一个单独的类,你应该有一个将是一个实际游戏对象的类thiszStateZombieStatezStateZombieStateSingletonZombieZombie

答: 暂无答案