- Home /
Question by
vezer · Mar 05, 2014 at 12:13 PM ·
instantiaterandomrandom spawn
How would 1 instantiate a cube on a random position on screen , ortographic camera(2d game)?
Well i think the title of the question speaks for itself, i am trying to instantiate a cube on a random position on my screen. If it's possible could someone help me in C# ?
Comment
Post the code you have written so far, and then ask specific questions about what isn't working.
Hint: Your solution should use either GameObject.CreatePrimitive or Object.Instantiate, and then Random.Range.
Answer by DaceProducer_Official · Jan 21, 2015 at 03:24 PM
I haven't tested this, but it should work.
public GameObject thePrefab;
void Update () {
GameObject clone = (GameObject)Instantiate(thePrefab);
clone.transform.position = Camera.ScreenToWorldPoint (Random.Range (0, camera.pixelWidth), Random.Range (0, camera.pixelHeight));
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613135422im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Random spawn Gameobject in area 2 Answers
Instantiate random object from Array 2 Answers
gameobject spawn in randomrange coordinates 1 Answer
instantiate random prefabs based on player camera distance 1 Answer
Random Spawn 2 Answers