x


Generating pickups randomly

First of all, I must warn you I am quite a newbie on Unity, so please excuse my dumb questions. Anyway, I am developing a 2D platform game (similar do Robot Unicorn Attack), and I need to generate pickup items (like coins) randomly on the platforms. Problem is I have no idea how to do that. I already made my pickups, but they are the kind we manually place them on the screen. What I want to do is during the game, once the platform is visible, generate the pickups randomly. Thank you very much for your help.

more ▼

asked Jan 11, 2012 at 05:38 PM

theJuls gravatar image

theJuls
16 3 2 2

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Make an array to hold your pickup prefabs, and place empty game objects above your platforms, where you want the pickups to be. You can make them children of the platform so it'll be easier to find them. Have a script on the platform that determine when the platform is visible, that holds the pickup locations in an array of it's own, then iterates over it and randomly chooses a pickup prefab from the pickups array.

You can hold the pickups array in an external class and reference it from every platform. Or you can have an array for each platform for greater control.

more ▼

answered Jan 11, 2012 at 05:48 PM

asafsitner gravatar image

asafsitner
3.3k 13 17 35

(comments are locked)
10|3000 characters needed characters left

Without knowing more about the way you are coding this, the best answer I can give you is to look at the following...

Instantiate

The Instantiate command will let you create objects in the game world on the fly.

Hope this is of some help.

-Larry

more ▼

answered Jan 11, 2012 at 05:56 PM

Larry Dietz gravatar image

Larry Dietz
547 24 23 35

(comments are locked)
10|3000 characters needed characters left
var prefab : GameObject;
var spawns = GameObject.FindGameObjectsWithTag ("spawn");
for (var spawnSpot in spawns)
    Instantiate (prefab, spawnSpot.transform.position, spawnSpot.transform.rotation);

There you go. Spawns an object at all places tagged spawn

more ▼

answered Jan 11, 2012 at 06:15 PM

Muzz5 gravatar image

Muzz5
1.3k 205 169 181

UnityException: You are not allowed to call this function when declaring a variable.

What To Do ????

Dec 20, 2012 at 04:33 PM ani2020.bhandari

hey , i tried using this piece of code.It doesn't just spawn one object , but multiple copies of it (like in an infinite loop)..what to do ?

Aug 02, 2013 at 08:44 AM rangapraveen
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x6598
x1012
x477
x12

asked: Jan 11, 2012 at 05:38 PM

Seen: 1784 times

Last Updated: Aug 02, 2013 at 08:44 AM