I want to make a list of buttons with a scroll bar from scripting and instantiate, associated with the distance between a series of gameObjects (the ones with the tag) with one object(control tower) . The script must rearrange each T time with an update float. I did half of the work, but i have one problem, the buttons dont setparent to another ui object and only spawn one. There is the code, it’s not finished:
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
public class MakeListOfButtons : MonoBehaviour {
public string Tag;
public GameObject ControlTower;
public GameObject PrefabButton;
private GameObject FinalButton;
void Start()
{
InvokeRepeating ("MakeTheList", 0, 5);
}
void MakeTheList ()
{
int N = 0;
GameObject [] Buttons = GameObject.FindGameObjectsWithTag (Tag);
int NTag = Buttons.Length;
GameObject [] ButtonsSorted = new GameObject[NTag] ;
float [] Distances = new float[NTag];
foreach (GameObject B in Buttons)
{
Vector2 heading = new Vector2 ((B.transform.position.x - ControlTower.transform.position.x),
(B.transform.position.y - ControlTower.transform.position.y));
float Dist = heading.sqrMagnitude;
Distances [N] = Dist;
N = ++N;
}
float [] fixDist = Distances;
Array.Sort (Distances);
foreach (float D in Distances)
{
N = 0;
foreach (GameObject B in Buttons)
{
if (fixDist [N]==D)
{
ButtonsSorted [N] = B;
}
N=++N;
}
}
Vector3 CloneOriPos = new Vector3 (0.0f, 8.43f, 0.0f);
N = 0;
GameObject [] FinalList= new GameObject[NTag];
GameObject Parent = GetComponent <GameObject> ();
foreach (GameObject i in FinalList)
{
GameObject T = Instantiate(PrefabButton, CloneOriPos, Quaternion.identity)as GameObject;
FinalList [N]=T.GetComponent <GameObject>();
FinalList [N].transform.SetParent (Parent.transform);
CloneOriPos.y = +2f;
N=++N;
}
}
}