Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Max 4 · Jun 12, 2011 at 11:44 PM · intrange

Making a plain number range

I know about Random.Range, but I would like to say "x equals 1 to 10", and using just Range doesn't work. What do I do?

[EDIT] Hmm, ok, maybe it would be better if I explained what I wanted to do. I want my enemies to drop ammunition when they die, but not every time. So I'm using Random.Range to get a number when the enemy dies, which I want the numbers of different types to ammo to be compared to. If they are the same, then spawn that ammo. However, I want some ammo to be more common than others, and the only way I could think of doing that is by making a list, to make the chance of the number getting picked larger.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by aldonaletto · Jun 13, 2011 at 01:10 AM

You can do the following:

   if (Random.Range(0,10)<chance){
     // drop ammo
   }

The chance variable controls how often the ammo will be dropped: chance=0 means never, chance=5 means 50%, chance=10 means always. The chance var can be set to different levels depending on each ammunition type.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joshua · Jun 13, 2011 at 01:20 AM 0
Share

Yep, this should do the trick.

avatar image aldonaletto · Jun 13, 2011 at 01:41 AM 0
Share

@Joshua, in this case, which version of the Random.Range the compiler will choose, integer or float? Or the same type of the chance variable?

avatar image Joshua · Jun 13, 2011 at 03:21 AM 0
Share

@aldonaletto I think it will output an int because the two input variables are also integers. I'm not sure though, I'll test this tomorrow. :)

avatar image Max 4 · Jun 13, 2011 at 07:30 AM 0
Share

Oh, good idea. Nice and simple.

avatar image
0

Answer by Joshua · Jun 13, 2011 at 12:09 AM

var x : int[] = new int[10];

for( var i : int = 0; i<10; i++ ){ x[i] = i+1; }

Read up on arrays here.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Max 4 · Jun 13, 2011 at 12:24 AM 0
Share

I want x to be a list of numbers, so basically "x=1, x=2...".

avatar image Max 4 · Jun 13, 2011 at 12:46 AM 0
Share

Hmm, I'm having trouble understanding exactly what to do. I was just trying to make a variable equal a list of numbers.

avatar image Kith · Jun 13, 2011 at 12:48 AM 0
Share

I'm really not following exactly what you want. Joshua's answer is exactly that, an array of numbers. Whenever you'd want the first number in that array, you'd use x[1]. x[2] will give you the second number. x[3] will give you the third. That seems like a list of numbers to me.

avatar image aldonaletto · Jun 13, 2011 at 12:51 AM 0
Share

What is the idea? Do you want to vary x from 1 to 10, one at a time?

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Number Range 3 Answers

Mathf.Clamp an int value 4 Answers

Does anyone know why this is wrong? 2 Answers

it is not possible to invoke an expression of type int 2 Answers

Checking an objects script to change an int on another script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges