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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ALEGOMan · Sep 25, 2014 at 06:20 PM · timertexturesrandom.rangeintnumbers

How to make a random occurrences using a timer?

The game I'm making is mostly about randomization, you enter the scene, a random texture appears on an object, here is the code but it changes every second, I want it to change every time you enter the scene, if I get rid of yield and press play, unity goes grey and crashes.

 #pragma strict
 
 var textures: Texture2D[]; // assign some textures in the Inspector
  
 function Start (){
     while (true){
         renderer.material.SetTexture ("_MainTex", textures[Random.Range(0, textures.Length)]);
         yield WaitForSeconds(2.0);
     }
 }

I also want it so when you enter a scene a random number is chosen and then it begins to count down to 0 before ending the game. When I press play the random number is placed, then it goes straight to 0 instead of counting down from said number. I hope this is not too much to ask i'm fairly new.

 #pragma strict
 
 var timer : int;
 
 function Start () 
 {
     timer = Random.Range(10.0, 20.0);
 }
 
 function Update ()
 {
     timer -= Time.deltaTime;
     Debug.Log(timer);
     
     if(timer <=0)
     {
         timer = 0;
     }
 }

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Eaneth · Sep 25, 2014 at 07:20 PM

Problem One: Don't use while loops, they've been known to crash Unity hard.

Problem Two: For the random events try this...

 var nextTime: float;
 var modifier : float;
 
 function Start()
 {
     nextTime = 0.0;
 }
 
 function Update()
 {
     //Get a random value
     modifier = Random.Range(10.0,20.0);
     
     //Set nextTime equal to current run time plus modifier
     nextTime = Time.time + modifier;
 
     //Check if current system time is greater than nextTime
     if(Time.time > nextTime)
     {
         //DO EVENT
     }
 }

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 ALEGOMan · Sep 25, 2014 at 11:04 PM 0
Share

This seems to make both variables randomize any 2 digit number, it's hard to deter$$anonymous$$e exactly what its doing, it seems to be counting up and randomizing at the same time over the 10,20 limit

avatar image
0

Answer by Habitablaba · Sep 25, 2014 at 06:57 PM

To help solve the timer going straight to 0, start by making it a float instead of an int.

To help with the texture issue, simply remove the "while(true)" part. With it there, you are attempting to change the texture every frame, and you'll never leave the start method!

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 ALEGOMan · Sep 25, 2014 at 07:18 PM 0
Share

Thanks for the help, it does count down now but the only problem I have now it that it has 8 tenths(0.00000000) ins$$anonymous$$d of just a whole number, and the counter keeps going into the $$anonymous$$us when I made the if statement for 'timer <=0' when it does this the counter counts down the last 3 tenths, pretty strange. http://puu.sh/bND5r/8dd7d4d049.png

avatar image Habitablaba · Sep 25, 2014 at 08:11 PM 0
Share

Yeah, that's floats for you, never quite what you want them to be.
A common solution for this is
timer = $$anonymous$$athf.Round(timer * 10) / 10;
This will give you one decimal points worth of precision, but will also keep your float from being something silly like 0.0000004
In addition, if you don't want the user to see the decimal places, you can cast it back to an int when you display it!

avatar image
0

Answer by rutter · Sep 25, 2014 at 07:03 PM

 while (true)

That's an infinite loop. It never ends.

If your code runs asynchronously (such as a coroutine), that's okay because the program will be able to run while the coroutine is paused.

When you delete all yield statements in a function, it's no longer a coroutine. The loop was always infinite; the only difference now is that it keeps control of the main thread, too.

Infinite loop + blocking main thread = your program stops running.

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 ALEGOMan · Sep 25, 2014 at 07:10 PM 0
Share

It was so obvious haha thanks!!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing leaf colors in C# 1 Answer

How can I store HUGE NUMBERS on unity? (256bit integer) 0 Answers

Trying to figure out the best way to have a series of ints that are dynamically numbered in sequence 2 Answers

How to create Timer animations? 1 Answer

Big Numbers - How to convert (k, M, a ) 4 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