- Home /
instantiating elements in UI/Canvas
Hey guys. Basically all i need to do is randomly spawn sprites that fall across the screen on the screen space overlay canvas. Instantiate needs a vector 3 as a position, but the canvas does not have a vector3 position just a rect transform. So either need to instantiate using rect transform (which doesnt seem like it woul dbe possible) or get the equivelent postion in the UI using a vector. Hope that makes sense.
Something like this
GameObject randomGerm = germs[Random.Range(0, germs.Length)];
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x) ,spawnValues.y,spawnValues.z);
Quaternion spawnRotation = Quaternion.identity; //new Quaternion ();
GameObject germSpawned = Instantiate (randomGerm, spawnPosition, spawnRotation) as GameObject;
But this will only spawn in world space not the UI/Canvas.. hope you know what im aiming at
thanks guys
Answer by pfreese · Mar 18, 2015 at 05:28 AM
The important thing is to parent the newly instantiated object to the canvas.
You can also instantiate the object using the single parameter version of Object.Instantiate():
public static Object Instantiate(Object original);
and then modify the transform of the returned object:
GameObject germSpawned = Instantiate(randomGerm) as GameObject;
germSpawned.transform.SetParent(canvas.transform);
germSpawned.transform.localPosition = spawnPosition;
germSpawned.transform.localRotation = spawnRotation;
Hey there.. Well germs is an array of gameObjects (the images have empty gameobjects as parents for animation purposes). Where do i set the parent? before setting the rectTransform components? Also that is still using Vector 3s, as spawnPostiin is a Vector3 and i cant use a vector3 to place a UI image in screen space.
Does the GameObject you are cloning have a plain old Transform or a RectTransform? Either can be used to set the position in the Canvas, but the interface will be slightly different for each. However, you will set the parent the same way:
germSpawned.transform.SetParent(canvasObject.transform);
Using the Transform component, you can set the X,Y values of the position; they should be relative to the center of the canvas. If the game object has a RectTransform (or you replace the transform with a RectTransform using AddComponent), then you can set the anchoredPosition and it will use the behavior as deter$$anonymous$$ed by the anchor$$anonymous$$in/anchor$$anonymous$$ax/pivot values.
You could rewrite those 4 lines as:
GameObject germSpawned = Instantiate(randomGerm, spawnPosition, spawnRotation) as GameObject;
germSpawned.transform.SetParent(canvas.transform);
but either way works just as well ;)
Your answer
Follow this Question
Related Questions
Screen space canvas not resizing correctly when android device is rotated 0 Answers
Scalewithscreensize equivalent for worldspace canvas's 0 Answers
UI Instantiate?,Ui, Sumonar UI? 0 Answers
Can't position instantiated objects on canvas 2 Answers
Instantiating UI element on Screen space - Overlay not instantiating exact specified location 0 Answers