## This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

• 0

# A random Spawn System

## Question

I need help with a borderlands type loot system. I have made 5 cubes, and named them common, uncommon, rare, very rare, and legendary.

I put the objects in to an array. I want common to be the most common chance of getting, and legendary to be the rarest. How would I go about doing that?

I'm new to programming, so don't make fun of me if it is easy.

Here is what I have so far:

Please login or register to see this code.

If you could help, that would be great.

Edited by Seabass121

## Recommended Posts

• 2

You can just use a random number in order to define the rarity of your objects. I explain to you my point of view

float object = Random.Range(0.0f, 1.0f); // defines randomly a float number between 0 and 1

if (object >= 0.95f) // an amazing object! Yeah! Lucky boy

Instantiate(goldObject, position, Quaternion.identity);

if (object >= 0.8f) // rare object

Instantiate(silverObject, position, Quaternion.identity);

if (object >= 0.5f) // no bad no good

Instantiate(bronzeObject, position, Quaternion.identity);

else

Instantiate(stoneObject, position, Quaternion.identity); // no luck

You said that you use an array? You can simply use one of its elements :

Instantiate(arrayObjects[arrayObject.length], position, Quaternion.identity); // last object in array

##### Share on other sites

• 0
Please login or register to see this quote.

Thanks so much gecko.

I plan on using the au fps kit to attempt to make a borderlands style game, but obviously not as complex.

##### Share on other sites
• 0
Please login or register to see this quote.

Ok, so here is my script now,

Please login or register to see this code.

Every Time I launch, It gives the random value, then when I click, it only gives me that chance throughout the entire thing until I launch again.

Edited by Seabass121

##### Share on other sites
• 0

Fixed! I put the variable in the wrong place. LOL.

##### Share on other sites
• 0

Don't waste your resource calling each frame your SpawnObject function. In the Update section instead, check an event on mouse  and then define a new float value ++

Please login or register to see this code.

##### Share on other sites
• 0
Please login or register to see this quote.

I plan on adding more to this, that is why I put a separate function for this. This is just a test anyway. Thanks.

Beginner question, how would it be different if I put this in just the update?

##### Share on other sites
• 0

In fact, you must wait an event - mouse, keyboard or other device (even another function). GetMouseButtonDown works on the frame when the button has been pressed. Each time when you press this button, you call again this code sequence. If you put this code in the update section, without any condition, you define a random number each frame - dozens of times each second ++

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.