![](/img/trans.png)
[英]Is there a way to outline a GameObject through a C# script in Unity 3D?
[英]Unity 3d & C#, if gameObject != this
我有这个代码:
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
}
}
}
}
我对“if (planet.= this)...”有一个问题,我期望发生的是,如果 planets[index] == the gameObject 那么“planet,= this”将返回 false? 但这不起作用,我该如何解决?
this
是一个关键字,它引用当前 object 的实例,在本例中为Gravity
class,因此每个GameObject
object 都不同于Gravity
实例。 您可以执行以下操作来访问附加GameObject
:
foreach (GameObject planet in planets)
{
if (planet != this.gameObject)
{
// Do magic stuff...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.