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

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;