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 TheDemin · Nov 23, 2014 at 05:14 PM · javascriptcrash

Program Crashes Unity

I am writing code that makes the player rise up into the air, and then 'glide' (still maintaining basic movement, while slowly falling back down) after they hit an item. The problem is, it crashes Unity every time I run it. As in, I have to forcequit out of it every time I try testing it.

 #pragma strict
 
 public var Speed = 10;// Speed at which the character rises/falls
 public var isHovering = false;// Boolean that checks if the character is in a 'hovering' state
 public var beginningHoveringTime = 5;//Time (in seconds) it takes for a character to rise to hover state
 public var endingHoveringTime = 5;//Time (in seconds) it takes for a character to lower back to the ground
 public var totalHoverTime = 30;//Total time the player hovers in the air

 // Use this for initialization
 function Start () {
     
 };
 
 function OnCollisionEnter(target : Collision){
     
     if (target.gameObject.tag =="Hover")
     {
         isHovering = true;
         Destroy(target.gameObject);
     }//end if
 };//end OnCollisionEnter
 
 
 // Update is called once per frame
 function Update () 
 {
     while(isHovering && beginningHoveringTime > 0) //checks if the bool has been triggered,                                        //and the beginningHoverTimer is not finished    
     {            
         beginningHoveringTime -= Time.deltaTime;
         
         if (beginningHoveringTime > 0)//start of hovering
                 this.transform.position = Vector3(0, 5, 0);//Makes the character rise

     };//end while
     
     totalHoverTime -=Time.deltaTime;//Timer starts ticking while the player hovers around
     
     rigidbody.velocity.y = -1;
     
     if (rigidbody.velocity.y < -5)
         rigidbody.velocity.y = -1;
         
     if (totalHoverTime <= 0 && isHovering)//Timer has completed, and the player will fall
     {
         isHovering = false;
         rigidbody.velocity.y = 0;
         
         endingHoveringTime -= Time.deltaTime;//makes sure the character falls at the same rate each time
         
         while (endingHoveringTime > 0)
             this.transform.position = Vector3(0, -5, 0);////Makes the character fall
     };//end if
     beginningHoveringTime = 5;//These two lines reset the timers for multiple uses
     endingHoveringTime = 5;
 };//end Update

This code is in Javascript, with a c# script that adds torque to the ball (to move it around). Is it the torque script being C# and this script being in Javascript the reason that is causing it to crash, and if so, is there any way to convert my script into C#, or vice versa? Thank you in advance for your help.

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

Answer by robertbu · Nov 23, 2014 at 05:24 PM

Your problem is on line 50 - 52:

  while (endingHoveringTime > 0)
          this.transform.position = Vector3(0, -5, 0);////Makes the character fall
  };

Once you enter this while loop, nothing changes 'endingHoverTime' and therefore your app hangs forever executing this while loop.

There are a number of issues here. First, when writing code like this, you want to do a bit of work every Update() call. Both the while loop on line 50 and the one on 27 are not the way you want to handle movement. Update() gets called once a frame, so you want to figure out how to do the movement for that frame.

Also there are other issues. For example:

   this.transform.position = Vector3(0, -5, 0);////Makes the character fall

...sets the absolute position of your game object. It does not make it fall. To make it fall consider using Transform.Translate(). And you want to explore the use of Time.deltaTime in your movements.

I recommend you take a step back and do some basic tutorial on movement and write some simple test scenes. After you have mastered the concepts of movement, come back and rewrite this code.

Comment
Add comment · Show 4 · 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 TheDemin · Nov 23, 2014 at 07:31 PM 0
Share

Dear robertbu,

First of all, thank you for your response.

Second of all, the reason why I used this.transform.position ins$$anonymous$$d of any other method was due to me thinking that the action of falling/rising was the reason why the program crashed, so I used that ins$$anonymous$$d of what I originally had, which was ridigbody.addforce. Does that make my code any better? Of course not, there are still plenty of issues with it that I must admit, I am a bit ashamed of missing before (This is why you have multiple people look at programs.)

Anyway, I will look into the tutorials, and come back with a better form of this code now that I understand where things broke. Thank you for your help.

Sincerely,

TheDe$$anonymous$$

avatar image MrSoad · Nov 23, 2014 at 08:02 PM 0
Share

You might want to get rid of all the unnecessary ";" as well, you don't need to put one after the closing "}" of if statements, functions, and while loops. :)

avatar image TheDemin · Nov 23, 2014 at 11:01 PM 0
Share

Here is the updated, functioning code, for anyone that is interested. Adjust the variables at your desire.

     #pragma strict

 public var jumpSpeed = 5;// Speed at which the character rises/falls
 public var isHovering = false;// Boolean that checks if the character is in a 'hovering' state
 public var gravity = 2;
 
 function OnCollisionEnter(target : Collision){
     
     if (target.gameObject.tag =="Hover")
     {
         isHovering = true;
         Destroy(target.gameObject);
     }//end if
 };//end OnCollisionEnter
 
 // Update is called once per frame
 function Update () 
 {
     Debug.Log(isHovering);
     if (isHovering)
     {
         rigidbody.AddForce(Vector3(0, jumpSpeed, 0));
         isHovering = false;
     }
     
     rigidbody.AddForce(Vector3(0, -gravity * Time.deltaTime, 0));    
     
 };//end Update
avatar image robertbu · Nov 23, 2014 at 11:25 PM 1
Share

@TheDe$$anonymous$$ - I suggest you move lines 20 - 24 into FixedUpdate(). Calling AddForce() in Update() will produce inconsistent results depending on the frame rate.

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

2 People are following this question.

avatar image avatar image

Related Questions

Script crashes Unity 1 Answer

A node in a childnode? 1 Answer

Unity webplayer crashing browser when it is loaded and unloaded into a page using JavaScript 0 Answers

Instantiate OnDestroy 1 Answer

Making changes to a terrian in game 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