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 ViperX · Nov 17, 2013 at 03:25 PM · crashwhile loop

Why does while make Unity crash?

Hi there,

About a week ago I knew nothing about Unity or even programming in any language so be easy on me..(=

I'm playing around with Unity and trying to get a Spine animation to stand idle when no key is pressed and to play a different run animation and move along the x axis when "right" or "left" are pressed.

I think I got the moving part right, in this code I tried disabling the idle animation and enabled the run animation whenever I press "right" or "left", for some reason Unity crashes every time I click play.

Thanks in advance!

using UnityEngine; using System.Collections; public class movePlayer : MonoBehaviour { public float speed ; private SkeletonAnimation run; private SkeletonComponent idle; void Awake () { run = GetComponent<SkeletonAnimation>(); idle = GetComponent<SkeletonComponent>(); } void Update() { float translation = Input.GetAxis ("Horizontal") * speed; translation *= Time.deltaTime; transform.Translate (translation, 0, 0); bool held = Input.GetButton("Jump"); while(held) { idle.enabled = false; run.enabled = true; } } }

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 Bunny83 · Nov 17, 2013 at 03:32 PM

It doesn't actually crash, it just does what you told your PC to do. Unity uses a single thread for scripting. So your PC happily is looping your two lines of code that are inside your while loop as long as "held" is true. Since held isn't changed inside the while loop it will never leave the loop.

Before you try something like this:

 while(held)
 {
     held = Input.GetButton("Jump");
     idle.enabled = false;
     run.enabled = true;
 }

That won't work either. The input (and everything else Unity does) is only updated once per frame. If you don't let Unity finish the current frame (with an endless while loop), nothing will be updated.

You might want something like this:

 bool held = Input.GetButton("Jump");
  
 if(held)
 {
     idle.enabled = false;
     run.enabled = true;
 }
 else
 {
     idle.enabled = true;
     run.enabled = false;
 }
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 ViperX · Nov 17, 2013 at 04:38 PM 0
Share

For some reason the run.enabled would not work but I think there is a better way to do this, ins$$anonymous$$d of having 2 animation scripts would there be a possibility to address the drop down within one?

This is how the script I imported from Spine looks:

alt text

How would I know the correct code for changing stuff in inspector within the code?

Thank you so much for the answer! Really helpful and understood!

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

18 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

Related Questions

wierd infinite while loop 1 Answer

Why does Unity crash ? 1 Answer

Why would this while loop crash Unity? 1 Answer

Massive memory usage during compile time 0 Answers

Unity 4.2.0f4 Crash. Why? 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