The transform.setParent is not working properly.
What I am trying to do: Repeatedly create UI object to be parented to the Canvas object (there are currently 5 in scene but only 1 is named Canvas)
What is currently happening: On start, the CreateMatcher works perfectly but any time after the start that it is called it does not function properly.
When it is created the second time it runs through CreateMatcher and throws the print statment into the console but still show the below image. It does not give any errors or warnings.
// Canvas game object that is use for creatematcher
public GameObject CanvasGameObject;
// match location is used for the UI canvas and is a specific location
Vector3 MatchLocation = new Vector3(0, 160, 0);
// Use this for initialization
void Start()
{
CanvasGameObject = GameObject.Find("Canvas");
//create first Perfect and Size Measure
if (GameObject.Find("Size Measure") == null && GameObject.Find("Perfect") == null)
{
CreateMatcher();
}
}
void CreateMatcher()
{
//finds the button using a name in an array
colorpull = GameObject.Find(combotestholder[buttonspressed].ToString());
//pulls the script varibles holder on the object assigns it colorpullscript
colorpullscript = colorpull.GetComponent<Varibles_Holder>();
// temp holder for a rectTransform because we are dealing with UI Objects
RectTransform rt;
// Instantiates a Object Perfect at vector and quneterion
Object PerfectGameObject = Instantiate(Resources.Load("Perfect"), new Vector3(0, 160, 0), Quaternion.identity);
// Assigns name Perfect
PerfectGameObject.name = "Perfect";
// debug statment to see that it has been found and will be assigning
print("found and assigning");
// Find perfect
PerfectScale = GameObject.Find("Perfect");
// Set parent to Canvas Game Object ref: Line 34
PerfectScale.transform.SetParent(CanvasGameObject.transform, false);
// Print assigned when Finished assigning
print("assigned");
// rect transform is assigned to prefectscales rect transform
rt = PerfectScale.GetComponent<RectTransform>();
// prefect scales location is equal to match location
rt.transform.localPosition = MatchLocation;
// Changes color
PerfectScale.GetComponent<Image>().color = colorpullscript.ColorUnderneathMe;
// Repeat above for Size measure
Object MeasureScaleGameObject = Instantiate(Resources.Load("Size Measure"), new Vector3(0, 160, 0), Quaternion.identity);
MeasureScaleGameObject.name = "Size Measure";
// measuring
MeasureScale = GameObject.Find("Size Measure");
MeasureScale.transform.SetParent(CanvasGameObject.transform);
rt = MeasureScale.GetComponent<RectTransform>();
rt.transform.localPosition = MatchLocation;
}