as @Landern pointed out, you need to transfrom the screen coordinates to world coordinates. For this conversion we will use the ScreenToWorldPoint method. This method takes screen coordinates not in a Vector2 format (as you would expect for the screen xy plane), but actually it also requires a “z” value that measure the z-distance to the camera position. This is because your UI camera is a perspective camera, and thus perspective projection applies from world → screen and screen-> world. In this projection what happens is that infinite number of world points are projected into the same screen point, therefore going backwards, if you request a world point from a screen point, without any other information, unity doesn’t know which world point associated to that screen point is the one you want. That is why the “z” component is needed.
If you have setup your camera, lets say at (0,0,-20), then requesting
Vector3 worldpos = camera.ScreenToWorldPoint( new Vector3(x,y,10) );
means that from the screen coordinates (x,y) the worldpos that you want, is the one that has a distance from the camera of 10. Since the camera is -20, worldpos will have z=-20+10 =-10.
So coming back to your problem, you have:
new Vector3(Random.Range(-Screen.width, Screen.width), Random.Range(-Screen.height, Screen.height), Random.Range(-14, 0))
That has two problems:
- you are not using ScreenToWorldPoint as mentioned before.
- you are using Random.Range(-14, 0), but your camera is also at (0,0,0) so some points will be actually be positioned at the same depth as the camera, which in this case will not be rendered (will be out of the view frustum)
To address both problems I will suggest to try something like this:
float UIPlaneZDepth = 10; //change as needed
Vector3 randomScreenPos = new Vector3(Random.Range(0,Screen.width), Random.Range(0, Screen.height), Random.Range(UIPlaneZDepth, UIPlaneZDepth + 14));
Vector3 randomWorldPos = Camera.ScreenToWorldPoint(randomScreenPos);
GameObject myBubble = Instantiate(bubble, randomWorldPos, Quaternion.identity) as GameObject;
As a final note, the line:
myBubble.transform.SetParent(panel.transform, false);
is correctly being used to place the new UI Object in the canvas hierarchy while maintaining the world position. So that is not your problem.