简体   繁体   English

统一 3d 和 C#,如果游戏对象 != 这个

[英]Unity 3d & C#, if gameObject != this

I have this code:我有这个代码:

public class Gravity : MonoBehaviour
{
    GameObject[] planets;

    // Start is called before the first frame update
    void Start()
    {
        planets = GameObject.FindGameObjectsWithTag("Planet");
    }

    // Update is called once per frame
    void Update()
    {
        foreach (GameObject planet in planets)
        {
            if (planet != this)
            {
                //do things
            } 
        }
    }
}

I have a problem with "if (planet.= this)..." what I expect to occur is that if planets[index] == the gameObject then "planet,= this" will return false?我对“if (planet.= this)...”有一个问题,我期望发生的是,如果 planets[index] == the gameObject 那么“planet,= this”将返回 false? But this is not working, how can I fix it?但这不起作用,我该如何解决?

this is a keyword which referes to the instance of the current object, in this case a Gravity class, so each GameObject object is different from a Gravity instance. this是一个关键字,它引用当前 object 的实例,在本例中为Gravity class,因此每个GameObject object 都不同于Gravity实例。 You can do something like this, to access the attached GameObject :您可以执行以下操作来访问附加GameObject

foreach (GameObject planet in planets)
{
   if (planet != this.gameObject)
   {
      // Do magic stuff...
   } 
}

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

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