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 /
  • Help Room /
This question was closed Jun 04, 2016 at 09:46 PM by SteelArrow21 for the following reason:

The question is answered, a simple mistake was made.

avatar image
0
Question by SteelArrow21 · Jun 04, 2016 at 05:17 AM · c#collidertriggercollidersontriggerenter

C# OnTriggerEnter Issue

This question gets asked a lot and I've searched for a solution but I can't seem to figure out why this function is not working. The trigger is not being triggered and they are on the same layer; no layers aren't able to collide with each other. I want the game to detect when the player walks through a wall. This script is in the wall:

 using UnityEngine;
 using System.Collections;
 
 public class wallScript : MonoBehaviour {
 
     void OnTriggerEnter (Collider player) {
         Debug.Log ("Triggered");
         if (player.gameObject.tag == "Player") {
             Debug.Log ("Player has passed through");
         }
     }
 }

This is the wall: alt text

This is the player: alt text

wall.png (13.9 kB)
player.png (18.3 kB)
Comment
Add comment · Show 4
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 allenallenallen · Jun 04, 2016 at 06:22 AM 0
Share

Exactly which part isn't working? The whole thing? Or just the player part?

player.tag is wrong. It should be player.gameObject.tag

avatar image SteelArrow21 allenallenallen · Jun 04, 2016 at 06:32 AM 0
Share

Thank you, I fixed the player.gameObject.tag, but the whole thing isn't working. The trigger is not being triggered.

avatar image Alec-Slayden · Jun 04, 2016 at 09:33 AM 0
Share

Just because it hasn't been asked yet: you are certain that the script is actually added onto the relevant objects in the scene, and any prefab prototyping in the scene is being applied before runtime instantiation (if you are doing runtime-instantiated prefabs), so it's not missing the script on the object when they collide?

avatar image SteelArrow21 · Jun 04, 2016 at 09:46 PM 0
Share

I would like to thank you all for your help with this issue; you all had great answers. Now I would like to apologize because after moving on to something else, I realized I wasn't getting any notifications. I checked, and I had my notifications disabled for some reason. Sorry to waste your time, and hopefully this will help someone who makes a similar mistake.

3 Replies

  • Sort: 
avatar image
1

Answer by krutpatel2257 · Jun 04, 2016 at 06:58 AM

Well the wall has two collliders one of which the MeshCollider is a non-convex one and i guess the rigidbody on the wall is non-kinematic. Unity 5 doesn't support a non-convex MeshCollider on a non-kinematic rigidbody gameobject. I guess that might be the problem. Try setting the MeshCollider to convex or if you don't need it simply remove it. Also, instead of finding the GameObject using player.gameObject.tag == "" use player.CompareTag(""), because later one compiles to fewer CIL instructions than the direct comparison i.e. it probably results in a single call to the CompareTag internal function, while the former alternative, hence giving better performance.

Comment
Add comment · Show 8 · 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 SteelArrow21 · Jun 04, 2016 at 07:02 AM 0
Share

Actually the wall has no rigidbody which I believe shouldn't be a problem according to the table at the bottom of this page: http://docs.unity3d.com/$$anonymous$$anual/CollidersOverview.html I tested the wall with a rigidbody as well with the mesh collider removed, but once again, nothing was bring triggered.

avatar image krutpatel2257 · Jun 04, 2016 at 07:08 AM 0
Share

Is the Triggered getting logged from Debug.Log ("Triggered"); ?

avatar image SteelArrow21 krutpatel2257 · Jun 04, 2016 at 07:14 AM 0
Share

No, it is not

avatar image krutpatel2257 SteelArrow21 · Jun 04, 2016 at 07:23 AM 0
Share

Try checking the transforms of both the objects whether they allow the collision. I guess you are making a 2d game on x-z axis, so check if both objects have same y coordinate. Its a silly thing to say but it may have happened.

Show more comments
avatar image krutpatel2257 · Jun 04, 2016 at 07:40 AM 0
Share

There is no problem with the wall or the player colliders and your code is fine. I guess you might be missing something small. $$anonymous$$aybe post an image of the scene.

avatar image SteelArrow21 krutpatel2257 · Jun 04, 2016 at 08:47 AM 0
Share

Here is the scene. In this picture, the player, which is the camera with the collider, just entered the box collider (the wall) which has "Is Trigger" set to active. The wall has a $$anonymous$$inematic RigidBody as well. alt text

scene.png (106.7 kB)
avatar image krutpatel2257 SteelArrow21 · Jun 04, 2016 at 11:19 AM 0
Share

I replicated what you did, and it worked for me. Similar to yours i have a Player with a collider and rigibody. The wall has $$anonymous$$eshCollider where convex is set to false, BoxCollider with isTrigger to true, the Script wallScript and no RigidBody.

avatar image
1

Answer by juicycleff · Jun 04, 2016 at 09:39 AM

@STeelArrow21 I believe I've had this kind of problem before. Here's what I think you should do as your code is Ok. Double check if your player character has the tag "Player". Just to be sure if it does put another tag and again set it back to Player tag. Most times the problem is with the tag comparison. Try debugging with this. If it logs then your problem is truly from you tags.

if (player) { Debug.Log ("Player has passed through"); }

Comment
Add comment · Show 2 · 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 Alec-Slayden · Jun 04, 2016 at 09:43 AM 0
Share

good advice! Though it was mentioned that the initial debug log is not showing in the console, which means the method may not be getting called at all

avatar image juicycleff Alec-Slayden · Jun 04, 2016 at 11:09 AM 0
Share

I saw that. What I meant by the code sample is to see if any tag can trigger it and not just the player. If no tag can trigger it, then he can cross out the Tag comparison. Having issues with 5.3.3 some times the tags just don't work figured a way passed it though just random trial and fail. I think it's a bug probably on the editor side

avatar image
1

Answer by MajorParts · Jun 04, 2016 at 04:42 PM

What about the layers of these objects? Do they have a layer set? Are the layers set up to collide in the physics matrix?

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

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

Calling "OnTriggerEnter" when a parent object has a rigidbody 0 Answers

OnTriggerExit2D doesn't work when one of gameObject setActive false. 0 Answers

Trigger enter and directly Exit 0 Answers

How to make an object move to the direction in which the user faces using vr gaze interaction? 1 Answer

Scripting Portal 0 Answers


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