- Home /
Instantiate a child with its tag
I've looked around at all the forums I can regarding this and I'm still stumped. My goal is to instantiate a field (with a bunch of objects including 3 player spawn tiles), click on a gui button and have a character spawn on P1T (if its available), then spawn two other characters on the other two tiles.
I've got quite a few levels that all sit as prefabs in the same scene. Its important that the objects are instantiated because of how the level progression works. However when I instantiate one of the fields the child objects lose their tags. And when I tried to create a script on the tiles to have them change their own tags when they Awake, the scripts aren't even components once the parent object is instantiated.
Does anybody know how I could assign them tags after they're instantiated or have the tags stay?
And once I'm able to spawn a character is there any way I could do a simple- if tag P1 exists on the hierarchy check for P2?
public class OnCharButtonClick : MonoBehaviour {
public GameObject PlayerPrefab;
private GameObject P1Tile;
public void ChooseChar(int level)
{
// button turns grey
// button becomes disabled
// Instantiate PlayerPrefab
// Check if tag P1
// if yes check if P2
// if yes check if P3
// if yes debug too many characters
// if no change PlayerPrefab tag to P3
// -Close Panel
// if no change to P2
// if no change to P1
P1Tile = GameObject.FindWithTag("P1T");
Instantiate (PlayerPrefab,P1Tile.transform.position,Quaternion.identity);
}
}
Answer by hbalint1 · Apr 21, 2015 at 09:37 PM
try this:
GameObject newObject = (GameObject)Instantiate (PlayerPrefab,P1Tile.transform.position,Quaternion.identity);
newObject.tag = "P1T";
On which script should I attach that? I instantiate the objects using a loader script that instantiates a random tree of objects:
using UnityEngine; using System.Collections;
public class Loader : $$anonymous$$onoBehaviour {
public GameObject Forest1;
public GameObject Forest2;
public GameObject Forest3;
public int raid1$$anonymous$$;
public int raid1max;
public bool newStage;
public bool raidOver;
private int randomLvl;
private int random$$anonymous$$ob;
// Use this for initialization
void Start () {
newStage = true;
raidOver = false;
}
// Update is called once per frame
void Update () {
if(newStage==true)
{
randomLvl=Random.Range(raid1$$anonymous$$, raid1max);
random$$anonymous$$ob=Random.Range(1,3);
Destroy(GameObject.FindWithTag("RoomObjects"));
if(randomLvl==1)
{
Instantiate (Forest1);
}
if(randomLvl==2)
{
Instantiate (Forest2);
}
if(randomLvl==3)
{
Instantiate (Forest3);
}
newStage=false;
}
if (raidOver == true) {
Application.LoadLevel(0);
}
}
}
sorry, i now read it again and i misunderstood the question. so somehow only the children lose their tags. taht shouldn't be happen. if you set the children's tag correctly, then make prefab from the whole hierarchy they should keep their tags
You're right. It was something stupid :P I edited the prefab in from the project folder ins$$anonymous$$d of the hierarchy. For anyone who runs into this issue in the future, you need to have the prefab in the hierarchy, then delete the original and replace it with your new one.