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 Taylord303 · Aug 11, 2013 at 07:46 AM · c#inputspritewhile

Change texture every nth second while key down

I'm trying to figure out how to change the texture of an object every nth second, and while it should be simple, I've had a terrible time trying to figure it out. I'm trying to use an array of textures as a sort of sprite-type animation on a flat plane/quad. I want the texture of that plane to change to the next texture in the array after every nth second while the player is holding down a key. Any help would be greatly appreciated. I'm trying to figure it out in C#.

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

Answer by whydoidoit · Aug 11, 2013 at 08:00 AM

You should start a coroutine when the button is pressed and have it verify the key is down, then do the wait. It could be written as a generic coroutine to work on anything as shown here:

   public Texture2D[] textures;

   void Update()
   {
         if(Input.GetKeyDown(KeyCode.Y))
         {
             StartCoroutine(ChangeTextures(renderer, textures, KeyCode.Y, 1));
         }
   }


   //This will change anything and so could be in a Utility class
   public static IEnumerator ChangeTextures(Renderer renderer, Texture2D[] textures, KeyCode code, float delay)
   {
         var textureToUse = 0;
         while(Input.GetKey(code))
         {
             renderer.material.mainTexture = textures[textureToUse];
             textureToUse++;
             if(textureToUse >= textures.Length) textureToUse = 0;
             yield return new WaitForSeconds(delay);
         }
         
   }
Comment
Add comment · Show 6 · 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 Taylord303 · Aug 11, 2013 at 09:28 AM 0
Share

Thank you for the extremely quick answer! I had actually tried this earlier, and while it does work, I seem to be having a problem with this still in that while the key is down, because it is being called in Update, it calls the coroutine for however many frames the key is down. This makes the textures just flash on the object extremely fast several times over.

avatar image Joyrider · Aug 11, 2013 at 09:51 AM 0
Share

In your Update's if, add a

 debug.Log("$$anonymous$$eydown detected");

to check if it is really called that often. But it shouldn't, Get$$anonymous$$eyDown only returns true, on the frame the key was pressed down, otherwise it is false;

avatar image Taylord303 · Aug 11, 2013 at 10:09 AM 0
Share

Hmm... I guess that isn't it. However, I continue to have that same problem where all textures in the array flash across the plane, waits the delay, then flashes the textures again. Any ideas?

avatar image Joyrider · Aug 11, 2013 at 10:14 AM 0
Share

Would it be possible you're launching several coroutines and that they are running simultaniously?

If you have a similar code to what whydoidoit wrote, your key's state is only checked once every second. if you released the key and pressed it again in between two checks... your first coroutine would still be running, and you would have launched a second one.

avatar image whydoidoit · Aug 11, 2013 at 10:23 AM 0
Share

That's a very good point. You could probably use a loop to check for the elapsed time and also the key release.

Show more comments

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to use Multi-Tap in New Input System for Running? 1 Answer

(C#) A better way to limit actions to once per button press? 2 Answers

Double tap mechanics and axis inputs 3 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