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
-1
Question by Jrocker2002 · May 24, 2014 at 06:57 PM · crashcrashing

Why does unity keep crashing when I use this script

Whenever I press Left Shift in play mode which is my running cue, Unity freezes and I need to use task manager to close it. Does anyone know what to do if this happens. Here is my script

pragma strict

ar Hands : Transform; var PlayAnimation : boolean = false;

function Update() { while(Input.GetKeyDown(KeyCode.LeftShift)) { PlayAnimation = true; if(PlayAnimation == true) { Hands.animation.Play("GlockRun"); } }

 if(Input.GetKeyUp(KeyCode.LeftShift))
 {
     PlayAnimation = false;
 }

}

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 LMan · May 24, 2014 at 07:49 PM

Well maybe it's the crappy formatting, but your syntax seems to be a little wonky- and while loops tend to be really touchy in the first place, so I would recommend getting rid of it if you can.

Try this-

 if(Input.GetKeyDown(KeyCode.LeftShift))//Get key down is only called on the frame where the key goes down- it's not continuous.
         {
             PlayAnimation = true; //Since you're using a bool, all you need is to change it once anyway!
         }
 
         if(Input.GetKeyUp(KeyCode.LeftShift)) // Same deal for get key up!
         {
             PlayAnimation = false;
         }
 
         if(PlayAnimation == true)
         {
             Hands.animation.Play("GlockRun");
         }

I haven't tested that, but I don't think it should crash on you anymore! The other possibility would be if you don't have an animation component on Hands. in that case Unity would hang for a long time and then maybe throw a fit about how it cant find the animation component.

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 Jrocker2002 · Jun 03, 2014 at 01:29 AM 0
Share

thank you! It works! :D

avatar image
0

Answer by loz999 · Jun 03, 2014 at 09:13 AM

It won't work because GetKeyDown is only true on the first update, then it is false. It is only triggered for one frame after which the held is true. There are 3 states, down, held and up. You should really not use PlayAnimation to do this but instead have the button held set to increase your speed by a run multiplier variable. Then in Animator window your transition between idle, walk and run animations should have parameters on them called speed and in the area to the right of the screen in inspector atomic conditions set speed for each animation to perhaps speed some walk value. That way, as soon as you press and release the keys( as long as you code for it) you will automatically transition from idle to walk to run to walk to idle

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

Answer by loz999 · Jun 03, 2014 at 09:11 AM

Sorry, didn't see this was answered already. The 2d controllers tutorial on here shows a really easy way to transition.

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

23 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

Related Questions

Unity Crashes when opened 1 Answer

DoWhile Loop is crashing Unity. Why? 2 Answers

A node in a childnode? 1 Answer

When I create a script on mac, unity crashes! 1 Answer

Every time I try and put a stage I imported from Blender onto the workspace, Unity crashes. How can I fix this? 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