• 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 Sensisensic · Jan 02, 2013 at 04:33 PM · javascriptcolliderplayercrash

Script crashes Unity

The following script is attached to the player and it crashed Unity.

 #pragma strict
 
 var invulTime : float;
  
 function Update () {
     invulTime -= Time.deltaTime;
     RespawnInvulnerability();
 }
 
 function OnCollisionEnter(Col : Collision){
     invulTime = 1;    
 }    
 
 function RespawnInvulnerability(){
     if (invulTime > 0 && invulTime < 1){
         collider.enabled = false;
         print("invul ON");
     }
     if (invulTime <= 0){
         collider.enabled = true;
         print("invul OFF");
     }
 }

I'm not that great with coding, so there's probably a better script to do t$$anonymous$$s with. But what t$$anonymous$$s does is it constantly counts down the invulTime below zero, w$$anonymous$$ch means that the collider is enabled. When the player gets $$anonymous$$t it sets the invulTime to 1, w$$anonymous$$ch means that the collider is disabled in the window between 1 and 0 of invulTime. When the player gets $$anonymous$$t it detracts a life from the player and resets the player's position to the start position of the player. T$$anonymous$$s script then gives the player a second of invulnerability to get going again.

It happens when I'm playing the game in the Unity Game window, the 3 lives are depleted (consequently the player is destroyed) and the Game Over GUI (with a couple of boxes and buttons, Time.timeScale is 0 as well then) is overlayed and $$anonymous$$t the play button again to stop playing. The $$anonymous$$tting of the play button crashes Unity. Also, $$anonymous$$tting the Retry button (Application.LoadLevel(Application.loadedLevel)) in the Game Over GUI crashes Unity as well.

The following error is displayed: Unity.exe caused an Access Violation (0xc0000005) in module Unity.exe at 001b:012bf610.

Is there somet$$anonymous$$ng wrong with t$$anonymous$$s script? Perhaps you have an alternative way of doing what I want to do, w$$anonymous$$ch possibly won't crash Unity?

I also tried the following, t$$anonymous$$nking the constant detracting of Time.deltaTime might be causing it, w$$anonymous$$ch didn't fix the issue.

 #pragma strict
 
 var invulTime : float;
 
 function Update () {
 
     if (invulTime <= 1 && invulTime >= 0){
         invulTime -= 0.01;
     }
 
     RespawnInvulnerability();
 }
 
 function OnCollisionEnter(Col : Collision){
     invulTime = 1;    
 }    
 
 function RespawnInvulnerability(){
     if (invulTime > 0 && invulTime < 1){
         collider.enabled = false;
         print("invul ON");
     }
     if (invulTime <= 0){
         collider.enabled = true;
         print("invul OFF");
     }
 }

Thanks for your time!

Comment
Add comment · Show 11
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 whydoidoit · Jan 02, 2013 at 05:02 PM 0
Share

Hmm it looks fine doesn't it - I presume that there is definitely a collider attached to the GO this script is on?

avatar image Sensisensic · Jan 02, 2013 at 05:04 PM 0
Share

Yep, there's a Box Collider attached.

avatar image Julien-Lynge · Jan 02, 2013 at 05:47 PM 0
Share

A couple background questions: have you verified that this particular script is what's crashing Unity? Is there anything else running at the same time that could be causing it? Have you tried commenting out the code piece by piece to determine which particular line is causing the crash?

avatar image Sensisensic · Jan 02, 2013 at 06:09 PM 0
Share

Yes, it is this script for sure. When I don't attach it to anything all runs fine. It is possible that due to another script it says "nope, I'm gonna crash on you", but that is difficult to pinpoint out. In no other script I'm interfering with turning on and off colliders or anything like that. It does the crashing thing as soon as the script recognizes that it needs to turn the collider off and on again. The collider.enabled = true part is a bit redundant as well I noticed. But it makes no difference.

avatar image wlad_s · Jan 03, 2013 at 12:03 AM 1
Share

Have you tried with "Rigidbody.detectCollisions" ?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by wlad_s · Jan 03, 2013 at 10:47 AM

In the comments above, we've come to the conclusion that crashes stop if rigidbody.detectCollisions is used instead of collider.enabled. I t$$anonymous$$nk collider.enabled is obsolete.

Posting t$$anonymous$$s answer so anyone with a similar problem can find a solution.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Object won't collide with Player unless Player is moving 1 Answer

Simple EXP Script In Javascript 0 Answers

Multiple Colliders Issue 3 Answers

Script doesn't find other script 0 Answers

Recording metrics? 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