- Home /
How to make an Application.quit on trigger?
I want to make it so that when my character touches the enemy's collider, (and in the case, it would be a sphere collider,) It automatically closes the game. So far, this is what I have an it will not work. The most It did was freeze the game for a second, then resumed playing. I have the code on my Main Camera, so it will happen with all of the enemies.
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if (hit.collider.name == "Sphere Collider")
{
Application.Quit();
}
}
What am i doing wrong?
I wasn't sure as the OP didn't specify, but it would probably be safe to assume it is in editor.
So sorry i didn't specify where it was! I've tried it on normal "windows" mode, Editor mode, and "Windows 64-Bit" mode. Nothing.
Answer by AlucardJay · Sep 20, 2012 at 05:45 PM
Quit is ignored in the editor or the web player.
http://docs.unity3d.com/Documentation/ScriptReference/Application.Quit.html
You need to play it in a build. For example PC and Mac Standalone :
http://docs.unity3d.com/Documentation/Manual/PublishingBuilds.html
http://docs.unity3d.com/Documentation/Components/class-PlayerSettings.html
Yes, I tried it on all available options just now to confirm it for myself. same thing. The game that I'm making is essentially done, besides this stupid trigger.
Well maybe check backwards to see where it is breaking. First thing to do (is it switched on?) is to check the trigger event. Place a Debug.Log as the first line in your collider hit function :
Debug.Log( "hit.collider.name = " + hit.collider.name );
does this return "Sphere Collider" ?
Debug.Log( "hit.collider.name = " + hit.collider.name );
function OnControllerColliderHit(hit : ControllerColliderHit)
{ if (hit.collider.name == "Sphere Collider")
{
Application.Quit();
}
}
Like that? If so, it says "unknown identifier: Hit"
No, just above if (hit.collider ..... inside the function =]
Answer by Kiloblargh · Sep 30, 2012 at 01:53 AM
Why are you trying to do this? It would be interpreted as a crash by the player and probably be rejected from the app store, if that was where you were trying to go with it.
I'm wanting something like this. I'm making a demo level for a term project and I just want my collision to end the test run. I currently have:
using UnityEngine; using System.Collections;
public class PlayerEvents : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "EndEvent")
{
Application.Quit();
}
}
}
But nothing happens...
Your answer
Follow this Question
Related Questions
Android app not closing complete. 2 Answers
Application.quit() not working on android 5 Answers
Loading player location in a scene 2 Answers
Why does my trigger work in the editor but not in the .exe? 1 Answer
onExit, onQuit, or anything like that? 2 Answers