- Home /
"Player" tag collider from other GameObject?
I have this script attached to a GameObject but I need it to work on the GameObject with the "Player" tag. Why is the player variable not working when it collides with the "Wood" tag?
function OnControllerColliderHit(hit : ControllerColliderHit) {
var player = GameObject.FindWithTag("Player");
if(player.collider.gameObject.tag == "Wood") {
WalkWood();
}
}
Answer by aldonaletto · Mar 23, 2013 at 03:56 PM
player.collider.gameObject.tag is the same as player.tag, or simply "Player"!
If you want to detect the type of the object the player is on, attach the script to the player and look at the hit object's tag in the hit structure:
function OnControllerColliderHit(hit : ControllerColliderHit) {
if (hit.gameObject.tag == "Wood") {
WalkWood();
}
}
But be aware that OnControllerColliderHit events occur all the time due to the collisions between the CharacterController and the ground, thus the function WalkWood will be called almost every physics cycle while the player is over an object tagged "Wood".
Sorry to revive this old question, but for any newer people who view this, there is a way to work around the constant calling of the function. just add a variable that manages if the function can be called. then make that variable false when the function is called. Example:`function WalkWood() {` if(canUse == true) {
//do whatever here
canUse = false;
}
}
Your answer
Follow this Question
Related Questions
Distance thing with gameobject ? 4 Answers
Collision detection problem 1 Answer
When all objects with a certain tag has been destroyed, load the next level! 3 Answers
Reaching a gameobject from its collider 3 Answers
difference between GameObject.FindGameObjectWithTag(Tag tag) and GameObject.FindWithTag(Tag tag) 3 Answers