- Home /
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 this with. But what this does is it constantly counts down the invulTime below zero, which means that the collider is enabled. When the player gets hit it sets the invulTime to 1, which means that the collider is disabled in the window between 1 and 0 of invulTime. When the player gets hit it detracts a life from the player and resets the player's position to the start position of the player. This 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 hit the play button again to stop playing. The hitting of the play button crashes Unity. Also, hitting 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 something wrong with this script? Perhaps you have an alternative way of doing what I want to do, which possibly won't crash Unity?
I also tried the following, thinking the constant detracting of Time.deltaTime might be causing it, which 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!
Hmm it looks fine doesn't it - I presume that there is definitely a collider attached to the GO this script is on?
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 deter$$anonymous$$e which particular line is causing the crash?
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.
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 think collider.enabled is obsolete.
Posting this answer so anyone with a similar problem can find a solution.
Your answer
Follow this Question
Related Questions
Collision, change skybox in game 1 Answer
Unity webplayer crashing browser when it is loaded and unloaded into a page using JavaScript 0 Answers
Simple EXP Script In Javascript 0 Answers
Recording metrics? 1 Answer
Program Crashes Unity 1 Answer