Set Gameobject to scene gameobject with tag

Hey community! I can’t figure this out at all. I am trying to get a Gameobject variable to set itself to the first object it finds with the tag “cage”.
I have this:

public var cage : GameObject;

function Update () {
	cage = GameObject.FindGameObjectWithTag("cage");
}

From what the docs tell me, this should work. However, with my game you can choose different playermodels to be in the game. The way this is handled is that there are several UI buttons, each button spawning a different prefab (playermodel). The skin selection works great, but each has a “cage” child object. When I go to the workbench and press “Fire3”, it should Set the cage to be active (by default it is inactive.) Because of this, It doesn’t seem to update the “cage” variable with the cage object (which is in fact tagged cage by the way.)
Any help on this? Thanks guys/gals :slight_smile:

Try GameObject.FindWithTag(“cage”);

Hi,

Sorry I didn’t get the whole but following are my assumptions. In hierarchy you have some thing like the following structure:

  • MainCamera
  • Canvas
  • PlayerSkin
    • GameObject(Tag:Cage)
  • etc
  • etc

If this is true. now you have number of ways to do so like you are doing is absolutely fine. The-Evster is also the alternate way I think ( I never used this ).

I have total 3 methods in my mind :

  1. They way you are going

  2. You can find the child by

    transform.FindChild (“ChildName”);

it will be like when the you need to active the cage just active the child when required.
Note: You need to take the reference first and then disable the child in Start/Awake method because you can’t find a child if it is disable.

  1. You need to make a common MonoBehaviour script to every player like:

Now take reference of the script and call ‘ActivateCage(true)’ when you need and ‘ActivateCage(false)’ when you want to make it disappear

I think I have cover all aspect. But if this is not your need or this ways doesn’t work then do share your hierarchy view and detailed code. I will try to connect you with Skype or teamviewr

Thanks

Alrighty everyone, I guess I solved my own problem. For those who have the same problem, this is what I did:
I placed the cage into the scene, right where the player spawns then Activated the cage. Then I made a script called “ChildCage” and attached it to my player prefab. The script parents the Player to the cage as soon as the player spawns, then deactivates the cage.

public var cage : GameObject;

function Start () {
	cage = GameObject.Find("cage");
	
	if(cage == null) {
		Debug.LogError("Cage is null!");
	}

	cage.transform.parent = this.transform;
	cage.SetActive (false);
	//Parented P1 to Cage
}

You would then modify the script to fit what you are trying to accomplish.
Thanks to The-Evster (who I cant @ for some reason) and @itsharshdeep for helping :slight_smile:

OK so there is a lot of things you might have missed. In an update

enemies = GameObject.FindGameObjectsWithTag("Enemy");

for example, will put all objects with the tag enemy into an array of objects. As you only have one object

enemy = GameObject.FindGameObjectWithTag("Enemy");

This would work for one object, now a few things you should check

  1. Is the gameobject definately tagged as “cage”
  2. Does the gameobject actually exist when the start() function is called?

@hellopeople0004