Getting all objects with a tag and inserting them into an array of gameobject

Hi! I am trying to get all the objects with the tag “Movable” from the scene and inserting them into an array of Gameobjects. i have looked over examples from documentation but it does not seem to be working. Here is my code:

public GameObject[] MovableObjects;

void Start()
{
  MovableObjects = Gameobject.FindGameObjectsWithTag("Movable");
  foreach(GameObject objects in MovableObjects)
  {
    Debug.Log(objects);
  }
}

I do not see anything in the console so clearly MovableObjects is empty.What am i doing wrong?

did you dibug.log moveableobjects just to make sure it is null

i just added this if(objects == null){ Debug.Log("null");} and it is not showing null

ok so upon adding more debug.logs i see it does not even enter the foreach loop

also MovableObjects is not null and if i try to see what it contains it returns nothing

maybe the tag is movable not Movable or another typo mistake like that

1 Answer

1

When you are performing your FindGameObjectsWithTag in your script are your movable objects active? If they are not active then they won’t be found.

yeah i typed it corectly on mono but typed it wrong here. :D

For the sake of it, try it using for loop.

i tryed that and just like here it does not even enter the loop

and my objects are instantiated at runtime and they also have corect tags on

Can you just post a screenshot of one of your Movable object. Just to see if anything is missing there.