I have a list and I’m trying to check if a gameobject is at a certain place in that list but I not sure how to check it .
public List<GameObject> Buttons = new List<GameObject>();
if (this.CompareTag ("Barracks") && this.gameObject == Buttons[2].gameObject) {
//Some code
}
Above just checks if the list[#].gameobject is the same type of gameobject as this gameobject but it doesnt check if its this specific gameobject. Thanks!!
“Above just checks if the list[#].gameobject is the same type of gameobject as this gameobject but it doesnt check if its this specific gameobject”. No, it doesn’t It check if it is the very same variable (unless it’s a primitive type (int, float…), string etc). To see this, try something similar to my code here, where the other gameObject is of your choosing. Point is that although they are both of the base class, GameObject, the == operator returns false.
I think the operator ‘==’ should work well in this situation, since it checks whether the two references (on the left and right side of the operator) refer to the same object.
Do you want to check if the gameobject (which has this script attached to it) is the same as the gameobject, which is in the Buttons list at the index of 2?