- Home /
[Solved] Exit scene OnCollisionEnter not working
I'm new to Unity (so i don't really know how to script properly) and i'm trying to do a FPS game. Everything works fine except that the script i'm trying to use to exit the application doesn't work. I've been looking through the forums and nothing seems to work. I know i'm doing something wrong but can't tell why; any help will be welcome.
The cube (goal) i'm using has a box collider with "Is trigger" unchecked and no other scripts attached. The character doesn't have a collider, but it detects platforms and walls, so i don't think this is the problem.
This is the script i'm using (javascript):
function OnCollisionEnter(collision : Collision) {
//Will display if collision was detected
print("Worked");
//Exits the game
Application.Quit();
}
Thanks for the help! :)
The script that solved my problem (thanks to darker9999):
function OnTriggerEnter (other : Collider) {
print("Worked");
Application.LoadLevel(0);
}
Cube(goal) is a rigidbody with Gravity unchecked, has a box collider with IsTrigger checked Graphics(Character) has a CharacterController
I assume that the player has a rigidbody attached. Does it have the is$$anonymous$$inematic property disabled?
I can't see any rigidbody attached but the default script on First Person Controler makes it work as if it had one.
Answer by darker9999 · Nov 28, 2012 at 06:36 AM
Application.Quit() will only work when you have built the game, I assume you are testing inside the unity editor at the moment.If the "Worked" is being printed its safe to assume everything is fine,for testing purpose I normally just reload the level.
The problem is, that i don't even get the "Worked" printed on the editor, also tried to reset level but it still doesn't work. $$anonymous$$aybe it has to do with the Unity version i'm using, which i wouldn't be surprised of.
Are you using a CharacterController? If so you will need to use function OnControllerColliderHit(hit : ControllerColliderHit), or you can use a trigger to get around this.
Triggers? Haha, don't have time to get involved in that :) Tried to use OnControllerColliderHit, i'll play a bit with options and maybe list all the things happening. Seems to work better than the other one though.
Also, i dropped a rigidbody onto the goal and got the level to reset with previous function. That means i can make the character push a block out of the way to end the game if i don't get this script to work.
Triggers are really easy, you could just tick is Trigger on the cube(goal) and then use:
function OnTriggerEnter (other : Collider) { print("Worked"); Application.LoadLevel(0); }
$$anonymous$$ake sure you have a rigidbody on one of the objects
$$anonymous$$y rigidbodies fall through the floor... I feel really stupid for not knowing almost anything about Unity. I will search it by myself though :) Thanks once again
Answer by DeveshPandey · Nov 28, 2012 at 05:38 AM
Hi TheFFFUUUguy,
Application.Quit() not working on editor, if you run your application on device after making a build then it will be worked :)
you can reset your game by using : function OnCollisionEnter(collision : Collision) {
//Will display if collision was detected
print("Worked");
//Reset the game
Application.LoadLevel(0);// 0 is the index of first scene, you can see this by pressing ctrl+shift+b in editor
}
Application.LoadLevel(0); or you can type the name of your scene ins$$anonymous$$d of the 0......
Thanks for making things clear, but i still can't get it to work (after changing script and build & run). I don't even get the "Worked" print, and that makes me think of an external problem such as script not being detected, misfunction or even object related properties.
there is no collision detected, may be you don't put the collider on the object. There may be a collider in the object ob which script is running and then if this will collide with any object then code will working...!!
so, check the collider on the objects and make sure this object collide with other :)
I have never seen "Worked" printed with this script, neither on editor or build version.
I have edited the my recent reply, so please see that and reply. Thanks!