Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ReflexGames · Apr 09, 2016 at 05:42 AM · javascriptcollisionontriggerentercube

On collision between two cubes

Ok so I have one cube on the ground, it has the trigger on, and there are other cubes with animations coming down from the sky. Pretty much if you don't tap the cubes on time, they fall down and hit the cube with the trigger. My code doesn't work for some reason, I have tried making the cubes falling from the sky the trigger with the code, but still no luck. Any Help? Thanks.

 #pragma strict
 
 function OnCollisionEnter (col : Collision)
     {
         if(col.gameObject.name == "Key1")
         {
             Application.LoadLevel(1);
         }
     }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by vyonox · Apr 09, 2016 at 07:03 AM

I think the previous reply is correct but I can add more details.

GameObjects with colliders have two different events for collisions: OnCollisionEnter and OnTriggerEnter. Both need a collider attached to the GameObject, but the first one is only for Colliders with the option "Is Trigger" not enabled. The second function is for Colliders with the option "Is Trigger" enabled.

As you may know, when a Collider with the option "Is Trigger" collides with another Collider, there is no physic reaction. There is no visible collision. The collision can be detected with the function OnTriggerEnter:

 void OnTriggerEnter(Collider other) {
     Destroy(other.gameObject);
 }

This can be used to detect when one GameObject reaches a position or touches another object and you want to collect the object. For example when the player touches a coin and the coin disappears. The function parameter is Collider other, that is basically the GameObject that touched this Collider. This option is a little strange if the GameObject is not destroyed or maybe it is transparent or something similar.

The other option is also perfectly valid. When the Collider has not enabled "Is Trigger", you have the function OnCollisionEnter. The two GameObjects will have a normal collision, with physic reaction, and you will know when this happens and also information of the collision (the parameter is Collision collision). This is useful to play a collision sound or create some special effect.

So, one or another depends of the kind of reaction you want when the two objects collide.

Comment
Add comment · Show 1 · 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 ReflexGames · Apr 09, 2016 at 08:25 PM 0
Share

Thank you for the input!

avatar image
0

Answer by JigneshKoradiya · Apr 09, 2016 at 05:49 AM

#pragma strict function OnTriggerEnter (col : Collider) { Debug.Log(col.gameObject.name); if(col.gameObject.name == "Key1") { Application.LoadLevel(1); } }

make trigger on of grounded cube and put this script on that dont check the trigger of other cube

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 ReflexGames · Apr 09, 2016 at 08:28 PM 0
Share

I am getting an error though

[1]: /storage/temp/67685-capture.png

capture.png (4.6 kB)
avatar image JigneshKoradiya ReflexGames · Apr 09, 2016 at 08:32 PM 0
Share

copy it again from here now i edited typing mistake

avatar image ReflexGames JigneshKoradiya · Apr 09, 2016 at 08:52 PM 0
Share

Ok I did what you said, and put the script on the ground cube and made it the trigger, but still nothing is happening.

Show more comments
avatar image ReflexGames · Apr 09, 2016 at 10:38 PM 0
Share

alt text

capture.png (10.8 kB)

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

66 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 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 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 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 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 avatar image

Related Questions

HELP PLEASE! 3 Answers

Check collision of childs Trigger in Parents script [JS] 1 Answer

How to check if car entered garage... then if true, do a specified function ? -1 Answers

Multiple OnTriggerEnter on One Collider 1 Answer

Trouble with a respawn 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