- Home /
Dynamical prefab, how to instantiate + positioning
Hey guys, I got a problem:
I'm doing a toggle field which will look like this:
My hierarchy is the following: Menu > (1)Panel > (2)Panel > Gameobject > Toggle > BG, Label
I got a prefabToggle and I positioned it both, outside and inside of the viewport / scene.
(2)Panel has a script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Chooser : MonoBehaviour
{
public GameObject MyButtonPrefab; //GameObject of PrefToggle is set here
public RectTransform ParentPanel; //(2)Panel
// Use this for initialization
void Start ()
{
GameObject newButton = (GameObject)Instantiate(MyButtonPrefab);
newButton.transform.SetParent(ParentPanel, true);
newButton.transform.localScale = new Vector3(1, 1, 1);
newButton.transform.parent = Camera.main.transform;
newButton.transform.position = new Vector3(0, 0, 0);
newButton.name = "Test";
newButton.GetComponentInChildren<Text>().text = "Test";
//Test1:
newButton.transform.parent = Camera.main.transform;
newButton.transform.position = new Vector3(0, 0, 0);
GameObject newButton2 = (GameObject)Instantiate(MyButtonPrefab);
newButton2.transform.SetParent(ParentPanel, true);
newButton2.transform.position = new Vector3(0, 40, 0);
newButton2.transform.localScale = new Vector3(1, 1, 1);
newButton2.name = "Test2";
newButton2.GetComponentInChildren<Text>().text = "Test2";
}
}
But unfortunately, the toggles created are not being shown in my panel. I tried it with Vertical Layout Group, which showed the toggles but their positioning was wrong on the one hand and dependant on the prefab Button, which leads to not showing the buttons again if I dragged it out of the scene, on the other hand. And is it possible to solute the image given with the Grid Layout Group, or would one has to set up a 4x4 field and e.g. T2 can't take 0 - [0][3], three "gridfields" ?
Your answer
Follow this Question
Related Questions
Would a spawnpoint with the subcomponent of RigidBody cause instantiate problems? 2 Answers
how to call the animation of prefab 0 Answers
Removing A Component From An Instantiated Prefab After X More Are Instantiated 1 Answer
Instantiated Object being auto-deleted 0 Answers
Issue Instantiating prefab in C# 0 Answers