Load resource into instanciated Game Object

Hello everybody,

My question seems to be simple but stuck me since several hours :frowning:
In my top down 2d game, my levels are formed by random prefab rooms instanced via Resources.Load method.
I am trying now to include random objects and enemies into a specific child Game Object of each room (named “Objects”)…in vain !

My last attempt produced the code below (simplified for the demonstration) :

GameObject roomObject = Resources.Load("RoomStructure", typeof(GameObject)) as GameObject;

foreach (Room room in rooms)
{

posX = room.GetX();
posY = room.GetY();

GameObject roomInstance = Instantiate(roomObject, new Vector3(posX, posY, 0), Quaternion.identity) as GameObject;

// add an item
GameObject myObject = Resources.Load("testItem", typeof(GameObject)) as GameObject;
GameObject objectInstance = Instantiate(myObject, new Vector3(10, 10, 0), Quaternion.identity) as GameObject;

objectInstance.transform.parent = roomInstance.FindWithTag("Objects").GetComponent<GameObject>.transform;
}

My error is “Member ‘GameObject.FindWithTag(string)’ cannot be accessed with an instance reference; qualify it with a type name instead”.

Someone could help me please ?

The error suggests that you should use “GameObject.FindWithTag(string)” instead of roomInstance.FindWithTag
I’m not entirely sure what you want to do, but sounds like you’re looking for “roomInstance.GetComponentInChildren”. Of course, that only works for components as far as I know (not tags), so you would have to use a specific component (such as a script) instead of a tag for it to work…

Thanks Rostam24 for your answer, i got mixed up Tag and Game Object Name indeed :sweat_smile:

I made a new test in naming each rooms in order to use the “Find” method :

roomInstance.name = "Room" + roomId;
...
objectInstance.transform.parent = GameObject.Find("Room" + roomId).transform;

This code works except one detail : My item is not always located in the “Objects” Game Object :frowning:

I have just search a solution on the web and the only method I have found to get the Game Object child wanted is to use a foreach on the items return by the “GetComponentsInChildren” method.

I find this approach “complicate”, i am surprised that Unity 3d doesn’t allow that easier, and I wonder now if add all my Game Objects directly in the root scene is not in fact the best method.

Could you tell me that you think about ?

(sorry for my pathetic english…)

So, this is probably what I would do (but I’m not very experienced with Unity either):

  • Create a script, call it RoomObjects
  • In this script, create a public gameobject for each gameobject in the room that you think is interesting. For instance:
    public GameObject SpawnPoint;
  • Take your prefab, drag it into the hierarchy, and attach the RoomObjects script to it. Then drag and drop the gameobject that you are using as a spawnpoint to the right position in the inspector.
  • Save your prefab.

Now, you can:

GameObject roomInstance = Instantiate(roomObject, new Vector3(posX, posY, 0), Quaternion.identity) as GameObject;
var roomObjects = roomInstance.GetComponent<RoomObjects>();

GameObject myObject = Resources.Load("testItem", typeof(GameObject)) as GameObject;
GameObject objectInstance = Instantiate(myObject, roomObjects.SpawnPoint.transform.position), Quaternion.identity) as GameObject;

But the exact implementation of this concept depends on what you want. If you like the idea of having a script that keeps track of interesting gameobjects, then this will work for you. If all you want is to keep track of a single position, then I guess there are easier ways :slight_smile:

It’s my first full game with Unity 3d so I don’t know yet all best practices. Today, i haven’t a room script (extending mono behavior) which manage the content of each room, so i can’t use your technique. But it’s an idea to be investigated, and i will reflect about it !

For information, I have found on a gloomy forum how to include a gameobject in a specific other gameobject : Just use slash !

objectInstance.transform.parent = GameObject.Find("Room" + roomId + "/Objects").transform;