- Home /
How to instantiate random object in a panel ?
Hi. I am quite new with Unity 3D and c#. I am still trying to find my way. The manual is really not helping, it looks it was created by people who knows Unity for people who knows Unity and don't look for answers. So, my problem is the following. I am looking for a complete step by step noob answer on how to instantiate different elements at random position in a panel on a UI giving the panel has a max offset and min offset. There are about 190 elements to instantiate in 32 different panels. Each element will have to be scaled in x and y and get a random rotation. The elements are buttons. I think I need to create one prefab and then after, it's darkness. I am trying to understand the syntax of the instantiate but it is really not clear where I need to put the x,y, the x scale, the y scale and the rotation.
public void Structure()
{
for (int i = 10 * blockSelected; i < 10 * blockSelected + Init_Block.blknumbStruct [blockSelected]; i++) {
if (strVisible [i]) {
Debug.Log (strname [i]);
GameObject buttonS = (GameObject)Instantiate (prefabStruct) as GameObject;
buttonS.transform.SetParent (structPanel.transform);
}
}
}
Here is the code, it is still showing nothing. I set prefabStruct and structPanel in the inspector. According to some recommendations, I added a Vertical Layout Group component to the parent panel structPanel. Nothing... Very annoying.
Answer by Denscrivent · Feb 06, 2017 at 05:52 AM
void instantiateRandomObject(){
//randomize 190 elements
//randomize 32 panels
//instantiate randomized element
//set parent to randomized panels
//get recttransform component and set x and y to random
//get recttransform component and set z rotation at random
//get button component and set event OnClick
}
hope it helps
We will try this. It is very well decomposed. I think my problem is co$$anonymous$$g from the "set parent to randomized panels".
Your answer
Follow this Question
Related Questions
Displaced UI RectTransform collider in some resolutions issue. 1 Answer
UI positioning problem 1 Answer
Force Unity UI element to refresh/update? 4 Answers
Button rect transform top 0 Answers