Enable and disable of prefab components
Hi Com,
I'm coding my first game and I have some troubble figguring out some code.
How the game works:
You(Player) can rotate an object to fit in some holes. Because it should be somewhat random I wanted to randomize it by instanciating a prefab with all the different holes and only activate one at a time. So with the help of the forum/manual I wrote some code but it works only partially (Instaciates always the same hole). Any idea how to fix it?
If a pic of my hirarcy might help you to figgure it out I will post one. But I wanted to keep this thread a bit smaller a first ;)
Thx :)
public GameObject prefab;
private int startLength = 5;
public float spacing = 25f;
private int randContainer = 0;
private int randForm = 0;
private int tmp = 0;
public void Start()
{
//Random player generated
RandContainer();
//5 Random obstacles are generated in dependece of player form
for (int z = 0; z < startLength; z++)
{
//Instanciate the prefab in a line with 25u spacing between
Vector3 pos = new Vector3(0, 0, z) * spacing;
Instantiate(prefab, pos, Quaternion.identity);
//0 is only a placeholder to fix the problem I went into
prefab.transform.GetChild(/*randContainer*/0).GetChild(RandForm(randContainer)).gameObject.SetActive(true);
tmp = randForm;
if (z < 0)
{
//Disable the old one => only one Child is active
prefab.transform.GetChild(/*randContainer*/0).GetChild(tmp).gameObject.SetActive(false);
}
}
//##Debugtools
//Debug.Log("Container" + prefab.gameObject.transform.childCount);
//Debug.Log("Form" + prefab.gameObject.transform.GetChild(0).transform.childCount);
//randContainer = RandContainer();
//RandForm(randContainer);
}
public void Update()
{
}
public int RandContainer()
{
//Container = Cross or Star / pass the value to RandFrom()
int randContainer = Random.Range(0, prefab.gameObject.transform.childCount + 1);
Debug.Log("Container " + randContainer);
return randContainer;
}
public int RandForm(int randContainer)
{
//Form = Cross1 or Cross2 / get the value from RandContainer()
int randForm = Random.Range(0, prefab.gameObject.transform.GetChild(randContainer).transform.childCount + 1);
Debug.Log("Form " + randForm);
return 0;
}
}
Comment
Answer by TR33 · Apr 25, 2017 at 05:39 PM
Ok. Fixed it myself. After spending 3 days waiting for moderation :/
public GameObject player;
public GameObject prefab;
private int startLength = 5;
public float spacing = 25f;
private int randContainer;
public void Start()
{
//Random player generated
randContainer = RandContainer();
player.gameObject.transform.GetChild(randContainer).gameObject.SetActive(true);
//5 Random obstacles are generated in dependece of player form
for (int z = 0; z < startLength; z++)
{
//Instanciate the prefab in a line with 25u spacing between
Vector3 pos = new Vector3(0, 0, z) * spacing;
//Disable all the masks before instanciating one
for (int i = 0; i < prefab.gameObject.transform.childCount; i++)
{
for (int j = 0; j < prefab.gameObject.transform.GetChild(i).transform.childCount; j++)
{
prefab.gameObject.transform.GetChild(i).transform.GetChild(j).gameObject.SetActive(false);
}
}
//Instanciating the mask and enable one mask
prefab.gameObject.transform.GetChild(randContainer).transform.GetChild(RandForm(randContainer)).gameObject.SetActive(true);
Instantiate(prefab, pos, Quaternion.identity);
}
}
public int RandContainer()
{
//Container = Cross or Star / pass the value to RandFrom()
int randContainer = Random.Range(0, prefab.gameObject.transform.childCount);
//Debug.Log("Container " + randContainer);
return randContainer;
}
public int RandForm(int randContainer)
{
//Form = Cross1 or Cross2 / get the value from RandContainer()
int randForm = Random.Range(0, prefab.gameObject.transform.GetChild(randContainer).transform.childCount);
//Debug.Log("Form " + randForm);
return randForm;
}