Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 TheFFFUUUguy · Nov 28, 2012 at 12:32 AM · javascripttriggeroncollisionenterexitapplication.quit

[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

Comment
Add comment · Show 2
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 BiG · Nov 28, 2012 at 02:59 PM 0
Share

I assume that the player has a rigidbody attached. Does it have the is$$anonymous$$inematic property disabled?

avatar image TheFFFUUUguy · Nov 28, 2012 at 03:54 PM 0
Share

I can't see any rigidbody attached but the default script on First Person Controler makes it work as if it had one.

2 Replies

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

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.

Comment
Add comment · Show 7 · 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
avatar image TheFFFUUUguy · Nov 28, 2012 at 02:48 PM 0
Share

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.

avatar image darker9999 · Nov 28, 2012 at 04:42 PM 0
Share

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.

avatar image TheFFFUUUguy · Nov 28, 2012 at 05:17 PM 0
Share

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.

avatar image darker9999 · Nov 28, 2012 at 05:23 PM 0
Share

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

avatar image TheFFFUUUguy · Nov 28, 2012 at 05:32 PM 0
Share

$$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

Show more comments
avatar image
1

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
 }
Comment
Add comment · Show 5 · 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
avatar image Maulik2208 · Nov 28, 2012 at 07:00 AM 0
Share

Application.LoadLevel(0); or you can type the name of your scene ins$$anonymous$$d of the 0......

avatar image TheFFFUUUguy · Nov 28, 2012 at 02:52 PM 0
Share

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.

avatar image DeveshPandey · Nov 28, 2012 at 03:41 PM 0
Share

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 :)

avatar image TheFFFUUUguy · Nov 28, 2012 at 03:52 PM 0
Share

I have never seen "Worked" printed with this script, neither on editor or build version.

avatar image DeveshPandey · Nov 28, 2012 at 04:09 PM 0
Share

I have edited the my recent reply, so please see that and reply. Thanks!

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.

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

Get a UserTrigger in Javascript - Eg for SmoothMoves 1 Answer

What’s wrong with my Safe Volume (safe zone) code? 0 Answers

Repeat a Animation on the same GameObject? 1 Answer

LoadLevel Health Question. 1 Answer

change this javascript to C# script? 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