• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Alp-Giray-Savrum · Aug 03, 2013 at 05:27 PM · instantiateobjectrandomseconds

One of the numbers as Second ?

Hello again, I want to ask a question about random methods. I want to create an object at random seconds.

Such like ;

1st object instantiates wit$$anonymous$$n 3 seconds 2nd object instantiates seconds between 3 & 6 seconds

how can i do that using random method ? sorry for my bad english.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jamora · Aug 03, 2013 at 05:37 PM

Yes, you can use either the System.Random class or Unity's own Random class.

To get a random number between any two numbers, say x and y ( x

So to make t$$anonymous$$s concrete:
your first object would get a random number as Random(0f,3f);
your second would be 3f + Random( 0f,(6-3)f );

The "f"s mean floats, in case you only want integers you should leave them out.

Comment
Add comment · Show 2 · 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 Alp-Giray-Savrum · Aug 03, 2013 at 06:15 PM 0
Share

I've changed my idea about code. I want to apply this like this code below:

 var randomNumber : int;
 
     
     
     function Update()
     {
         
             
             
                 ChosenOne();
             
     }
     
     
     
     function ChosenOne()
     {
     
         randomNumber = Random.Range(1, 4);
         
         Debug.Log(randomNumber.ToString());
         
         yield WaitForSeconds(3 + randomNumber);
     
     }

how can i apply this as you said ? Could you edit ? thanks a lot :)

avatar image Jamora · Aug 03, 2013 at 07:25 PM 0
Share

My answer explains exactly how to achieve a random number from what ever continuous range you choose. I will try to explain further, but not write your code for you.

You must first know how much of your value is random and how much will always happen. If you want to get a value between 3 and 6; 1, 2 and 3 can be considered as always happening, and the random element is either 0,1,2 or 3. Because the first 3 always happen, you only need to add the random element (0 to 3) to it.

You need to read my answer(s) again and then apply the explained basic principle to your problem.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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 avatar image

Related Questions

Instantiate Prefab at random 1 Answer

Instantiate Object at random position relative to another object 2 Answers

Spawning objects at points within another object? 1 Answer

Spawning objects? 1 Answer

Objects Instantiate 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