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 Swize · Sep 10, 2018 at 09:51 AM · 2d gametimer2d sprites

Random timers in a 2d game

I have a game object that every couple of seconds i want it to change to change state, then the timer starts again and the loop is repeated till the game is closed. I got the sprite switching animation working but i cant find a solution to this sort of timer.

Thanks in advance

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
1
Best Answer

Answer by theInsomniacGameMaker · Sep 10, 2018 at 10:05 AM

Hi @Swize! There are a couple of ways you can approach this problem. I will tell you the easiest way.

Use InvokeRepeating.

InvokeRepeating is a function that keeps on calling a certain function after every x-seconds.
This is the link to the scripting reference.

Use this as a reference to achieve the desired effect.

I would suggest calling InvokeRepeating in the Start function (like they do in the example).

The InvokeRepeating function takes 3 parameters.

  1. Name of the function that is to be called.

  2. Delay for calling the function for the first time.

  3. After the first call to function what should be the time interval.

Feel free to ask me any other questions you have!

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 Swize · Sep 10, 2018 at 06:06 PM 0
Share

Thanks a lot ill will definitely use this :D, but how would i get the character to change back to his previous state, because i assume that after the timer will finish he will remain in the state that is declared in the function.

avatar image theInsomniacGameMaker Swize · Sep 14, 2018 at 10:05 AM 0
Share

Sorry for the late reply. Could you make another thread of question and explain the second problem in detail so I can help you. Thanks!

avatar image
0

Answer by Hellium · Sep 10, 2018 at 10:08 AM

 private float timer = 0 ;
 public float Duration = 10;
 public float[] Steps = new float[]{2, 4, 6, 8};
 private int stepIndex ;
 
 private void Update()
 {
      timer += Time.deltaTime ;
      if( timer > Duration )
      {
            timer = 0 ;
            stepIndex = 0 ;
            // Timer has looped : do something
      }
      else if( stepIndex < Steps.Length && timer > Steps[stepIndex] )
      {
            stepIndex++ ;
           // Step : do something
      }
 }

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

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

Related Questions

How to make "circle" sprite’s border more smooth? 1 Answer

Normal map on 2D sprite screwed it up.,Applying material with normal map on 2D sprite screwed it up. 2 Answers

Adding items to an inventory by clicking on them? 1 Answer

My player is not rotating upwards when it moves upwards. Any advice? 1 Answer

How to destroy a object when it's off screen and re-instantiate it when it gets back on screen 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