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? 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. You can do something like this, to access the attached GameObject
:
foreach (GameObject planet in planets)
{
if (planet != this.gameObject)
{
// Do magic stuff...
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.