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 Baunie · Jul 28, 2020 at 08:03 PM · scripting problemerrorcrashnewbie

Why does Unity crash with this code ?

I was trying to implement an idle walk for my npc. The implementation is quite rough since I am a beginner and i know that there is probably a better way but this was the only way i could think of.

Here´s the code :

 public class Npc : MonoBehaviour
 {
     // Initializing the timer and the time it takes for the NPC to choose where to go in this case 5 seconds.
     float timer = 0.0f;
     float timerMax = 5.0f;
    
     // Start is called before the first frame update
     void Start()
     {
        
     }
 
     // Update is called once per frame
     void Update()
     {   
         timer += Time.deltaTime; // Counting to 5
         
         Vector3 stepNegative = new Vector3(-0.5f, 0f, 0f); // Steps in either negative x or positive x so the movement looks better and the npc doesn´t teleport
         Vector3 stepPostive = new Vector3(0.5f, 0f, 0f);
        
         
         Vector3 randomDirection = new Vector3(Random.Range(-10f, 10f), 0f, 0f); // Choosing a spot on x Axis for the NPC to go 
 
 
         if (timer >= timerMax)
         {
             
             if(randomDirection.x > transform.position.x) // Checking if the "randomDirection" is to the left or the right and taking either negative or positive steps accordingly
             {
                 while(randomDirection.x != transform.position.x * Time.deltaTime)
                 {
                     transform.position += stepPostive * Time.deltaTime;
                 }
 
             }
             else
             {
                 while (randomDirection.x != transform.position.x)
                 {
                     transform.position += stepNegative * Time.deltaTime;
                 }
 
             }                       
           // Note: " * Time.deltaTime "  was added because i saw it in another movement script but i dont know what it does or if it is usefull in this situation
         
         }
         
     }
 
    
 
 
 
 }

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 tuinal · Jul 28, 2020 at 09:16 PM

NB Unity does not crash per se, it's just in an infinite loop of your own design. Update needs to complete for the next frame to happen; you're attempting to 'move' something incrementally in update (i.e. within a single frame), when you need to instead think in terms of these individual frames happening (i.e. move it once towards the target by x distance).

A 'while' loop in update is usually a mistake, as there are very few use cases where you'd deliberately want to iterate a process until a condition within a single frame, because conditions within a single frame (i.e. positions, user input, etc.,) are static.

You can recover it by opening your IDE (e.g. Visual Studio) and stopping the execution there.

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 Baunie · Jul 28, 2020 at 09:50 PM 0
Share

At first I didn´t quite understand what you meant by "in terms of individual frames happening" not because it wasn´t explained very well but because I am not very smart. Now, after reading it a few times, I realized what I did wrong and basically everything works now.

Thank you very much. Your well explained answer helped a lot.

avatar image
1

Answer by deniskotpletnev · Jul 28, 2020 at 08:15 PM

I don't quite understand why the while loop is needed here:

     while (randomDirection.x != transform.position.x * Time.deltaTime)
    {
               transform.position += stepNegative * Time.deltaTime;
    }

and here

  while (randomDirection.x != transform.position.x)
    {
             transform.position += stepNegative * Time.deltaTime;
    }


Try to remove it, most likely an infinite loop will form, because the condition of the while loop is never met. If so, then just change the conditions

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

274 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 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 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

Spawn script on mesh crashes Unity 0 Answers

I dont understand why the script to read .xml doesnt works. 1 Answer

Can anyone provide some help with that error ? 1 Answer

an unhandled win32 exception occurred in unity.exe [26640] 2 Answers

Unity freezes on play 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