The question is answered, a simple mistake was made.
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:
This is the player:
Exactly which part isn't working? The whole thing? Or just the player part?
player.tag is wrong. It should be player.gameObject.tag
Thank you, I fixed the player.gameObject.tag, but the whole thing isn't working. The trigger is not being triggered.
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?
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.
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.
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.
Is the Triggered getting logged from Debug.Log ("Triggered"); ?
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.
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.
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.
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.
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"); }
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
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
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?
Follow this Question
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