Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 runvsdawdle · Mar 14, 2013 at 10:56 PM · javascriptrandomizepoints-counter

Every 0.5 sec there is a 10% chance of a target spawning.

I need help with this. I'm super new to Unity and scripting and I need to make it so a game has about 5 different colored targets (all worth different points) that must have a 10% chance of spawning ever 0.5 seconds. How would one go about doing this do you think? Any tips and tricks are helpful. Thanks!

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

Answer by robertbu · Mar 14, 2013 at 11:15 PM

Untested code, but something like this:

 function Start() {
   InvokeRepeating("SpawnChance", 0.5, 0.5);
 }
 
 function SpawnChance() {
    if (Random.Range(0,10) == 0)
       // Spawn something
 }

You could have multiple different spawn changes SpawnChangeRed(), SpawnChangeBlue()... And use InvokeRepeating() on each.

Comment
Add comment · Show 1 · 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 Great-Peter · Nov 10, 2015 at 07:21 PM 0
Share

Very nice!

avatar image
0

Answer by Seth-Bergman · Mar 14, 2013 at 11:24 PM

Each of these features must be separated and understood individually:

1create a timer

2create a "decision" based on a percentage (10%)

3spawn an object

lets start with the timer.. luckily Unity gives a good method for this one, InvokeRepeating

the link gives a pretty good example to get you where you want.. (no need to put this inside Start, as the previous answer suggests..)

next for the "10% chance":

we can simply use a random number for now:

   if(Random.Range(0,10) == 5)

for example, would be all you need

then for the spawn, use Instantiate.... so here's a sample script (javascript):

 var myObjects : GameObject[] = new GameObject[5]; 

 InvokeRepeating(SpawnStuff,0,.5);

 function SpawnStuff(){
       if(Random.Range(0,10) == 5){
     var getRandomObject = Random.Range(0,myObjects.length);
     Instantiate(myObjects[getRandomObject],transform.position,transform.rotation);
     }

 }

this example still spawns everything in the same place though, you will need to figure out how to stagger the position (you can use a similar random number within the desired range, on the position var x,y, or z position....

by the way, myObjects is an ARRAY, for now you can view it in the inspector and add your objects to its slots there.. this will give you an idea of what an array is, and why it is so useful!

Comment
Add comment · 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

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

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Marching Cubes for Unity in JavaScript [Concept] 1 Answer

Problem with Javascript updating variables. 1 Answer

Sprint Error script? 2 Answers

Analysing player metrics help 0 Answers


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