- Home /
Random Script Failure
I have noticed multiple times that while I am editing other parts of my level in Unity, I will test it out and find out that the scripts in the doors no longer seem to be active/working. The only way I have found to fix this is to delete the door and replace it from the prefab. Attempting to revert to the prefab does not work. I am editing objects that are seemingly unrelated to the doors themselves. Does anyone know why this is happening and/or how to fix it? It is currently quite a pain to replace the doors every few minutes.
Here is the script if it helps:
#pragma strict
function OnTriggerEnter (obj : Collider) {
var thedoor = gameObject.FindWithTag("SF_Door");
thedoor.animation.Play("open");
}
function OnTriggerExit (obj : Collider) {
var thedoor = gameObject.FindWithTag("SF_Door");
thedoor.animation.Play("close");
}
I don't work in JS but "obj" might be a reserved word? Also, you are not actually using the variable, ala
if(obj.name == "Door")
Is "gameObject" a typo here, i.e. are you using GameObject.FindWithTag (uppercase G)
It is searching for the object SF_Door within the children of the gameObject.
Answer by tanoshimi · Jan 04, 2015 at 08:39 AM
If this script is attached to the doors (which also have a trigger collider attached), and you want them to play the open/close animation whenever a collider enters that trigger, you should just be able to do:
#pragma strict
function OnTriggerEnter (obj : Collider) {
animation.Play("open");
}
function OnTriggerExit (obj : Collider) {
animation.Play("close");
}
I didn't actually write this specific script however, it does work some of the time but randomly seems to fail ever few $$anonymous$$utes of editing.
I actually fixed my own issue. The referencing by tag did not work when I had multiple doors in the scene. I ins$$anonymous$$d changed it so that the script looks like this:
#pragma strict
public var thedoor: GameObject;
function OnTriggerEnter (obj : Collider) {
thedoor.animation.Play("open");
}
function OnTriggerExit (obj : Collider) {
thedoor.animation.Play(“close”);
}