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 /
This question was closed Jul 03, 2014 at 01:24 PM by Prasanna for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Prasanna · Jun 12, 2014 at 06:16 AM · colliderprefaboncollisionenterboxcollider

Problem in Collider

Hello there, In my game, i have to collect my keys. So i have a Key Prefab with Box collider, and have Character Prefab with Character Controller, the Player tag is "Character", Key name is "Key". I have used OnColliderEnter function to destroy that key. But it doesn't work. Here is the Snippet Code.

 void OnCollisionEnter(Collision Others)
     {
         if(Others.gameObject.name == "Key")
         {
             Debug.Log ("Key Collected");
             Destroy (Others.gameObject);
             Key_Text_Effect_Temp = Instantiate (Key_Text_Effect, new Vector3(transform.position.x, transform.position.y + 0.2f, transform.position.z + 0.5f), transform.rotation) as GameObject;
             Key_Texture_Time += Time.deltaTime;
             if(Key_Texture_Time >= 2)
             {
                 Destroy (Key_Text_Effect_Temp);
                 Key_Texture_Time = 0;
             }
         }
     }

I have aa other Object with Box Collider, its working fine with Character Object.

-Prasanna

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

  • Sort: 
avatar image
2
Best Answer

Answer by Andres-Fernandez · Jun 12, 2014 at 06:44 AM

Have you checked that the name of the other gameobject is exactly "Key"? Because if you are instantiating them they may have names like "Key(clone)". Also, setting the key collider as isTrigger will help.

I suggest you use a layer for items (or even just for keys). That way, whenever you hit an object within that layer you don't have to worry about the names.

Comment
Add comment · Show 4 · 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 Prasanna · Jun 12, 2014 at 06:50 AM 0
Share

@Andres Fernandez.., sorry About that line, i changed if(Others.gameObject.tag == "$$anonymous$$ey") ins$$anonymous$$d of if(Others.gameObject.name == "$$anonymous$$ey"). It gives same result, not for this only; i created one new cube(default gameobject), and using like this, but this too giving the same thing.

avatar image Andres-Fernandez · Jun 12, 2014 at 07:02 AM 0
Share

If you are using tags make sure the key has tag "$$anonymous$$ey" (also I recomend using function gameObject.CompareTag) Have you checked the layer both objects are in? They need to be checked in the Collision $$anonymous$$atrix.

avatar image Prasanna · Jun 12, 2014 at 08:47 AM 0
Share

I checked all methods(Collision using Layers, Collision using Tags, Collision using Names), but it doesn't work.

avatar image Andres-Fernandez · Jun 12, 2014 at 08:55 AM 0
Share

If the collider is trigger, method should be OnTriggerEnter. Check the example, it's exactly what you should be using.

avatar image
1

Answer by Prasanna · Jun 13, 2014 at 05:36 AM

@Andres Fernandez, thanks for your advise. I found the Solution, when the Character have Character Controller Component, the Syntax should be like this.

 void OnControllerColliderHit(ControllerColliderHit Others)
     {
         if(Others.gameObject.tag == "Key_New")
         {
             Debug.Log ("Key Collected");
             Destroy (Others.gameObject);
             Key_Text_Effect_Temp = Instantiate (Key_Text_Effect, new Vector3(transform.position.x + 3.0f, transform.position.y + 0.2f, transform.position.z + 0.5f), transform.rotation) as GameObject;
             Key_Texture_Time += Time.deltaTime;
             if(Key_Texture_Time >= 2)
             {
                 Destroy (Key_Text_Effect_Temp);
                 Key_Texture_Time = 0;
             }
         }
     }

So this will Help your Character to Collide with other Collider like Box Collider, Capsule Collider, etc..,

-Prasanna

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

22 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

Related Questions

How to Give OnCollisionEnter Priority for 2 prefabs ? 1 Answer

how to disable the character controller and box collider for after hitting the character controller 1 Answer

OnCollisionEnter won't fire? 2 Answers

How to detect collisions in C# 2 Answers

Collision Not Responding 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