There are 3 objects:
Player (tag is named as Player) - has box collider, no trigger
Cylinder (no tag name) - has capsule collider, is trigger checked
Plane (no tag name) - has mesh collider, no trigger
I added this code to Cylinder:
function OnTriggerEnter (other:Collider)
{
if (other.gameObject.tag == "Player")
{
Destroy(other.gameObject);
Application.LoadLevel (0);
}
}
What do I want is:
When the Cylinder collides Player, it restarts to the game level 0.
However, the game restarts everytime when Cylinder collides the Plane.
So, it just keeps on restart and never touch the Player.
How should I fix it?
Put some console debug in there and see what is going on (i.e., what is hitting what with what tags in play):
function OnTriggerEnter (other:Collider)
{
// See what is happening in this function
// ** REMOVE ** from final product code
Debug.Log(gameObject.tag + " entered Trigger tagged " + other.gameObject.tag);
if (other.gameObject.tag == "Player")
{
Destroy(other.gameObject);
Application.LoadLevel (0);
}
}
Just be sure to remove all Debug.Log or print statements from your final product, they are no good for fps.
It shows
Untagged entered Trigger tagged Untagged
The cylinder object is in hierarchy and I added the script to it.
The player is drag from a prebabs and has Player tag.
how should I fix it?
Dear Sir
If I am using function OnTriggerEnter (other:Collider)
which attached to the enemyObject, when it hits character, character will die.
I am not sure whether I am wrong or not.
Or, should I use function OnControllerColliderHit(hit:ControllerColliderHit) instead?
There is “Player” in the element of tag, why it still shows:
Untagged entered Trigger tagged Untagged ?
and why
if (other.gameObject.tag == "Player")
{
Destroy(other.gameObject);
Application.LoadLevel (0);
}
still execute? Even if Untagged, it should not run that code insides the block.
:?:
Is the cylinder a rigidbody? If so is it possible you have some other code that is restarting the level when that collision is happening?
You mentioned the cylinder is “in heirarchy”, do you mean it is a child of another object(s)?
Also, when you call Application.LoadLevel() all game objects are destroyed before loading the new level so you don’t need to call Destroy to kill the player.
Yes, Cylinder is rigidbody.
It is a game object, but not a child of a game object.
I attached this code to Cylinder:
function FixedUpdate () {
rigidbody.AddForce (-0, -10, -20);
}
function OnTriggerEnter (other:Collider)
{
Debug.Log(gameObject.tag + " entered Trigger tagged " + other.gameObject.tag);
if (other.gameObject.tag == "Player")
{
Destroy(other.gameObject);
Application.LoadLevel (0);
}
}
I do not understand why Cylinder touches plane and the game restart.
I know it’s obvious, but are you sure the plane hasn’t been given the Player tag by mistake?
-
I double checked it, plane has no tag.
-
Since the GameObject has a Player name already, so I changed the tag “Player” to “Character”
-
I think the logic of the code is wrong, but I am not sure
function FixedUpdate () {
rigidbody.AddForce (-0, -10, -20);
}
function OnTriggerEnter (other:Collider)
{
Debug.Log(gameObject.tag + " entered Trigger tagged " + other.gameObject.tag);
if (other.gameObject.tag == "Character")
{
Debug.Log(gameObject.tag + " entered Character tagged " + other.gameObject.tag);
Destroy(other.gameObject);
Application.LoadLevel (0);
}
}
function FixedUpdate and function OnTriggerEnter cannot co-exist in one script.
I double checked it with debug log, actually these 2 lines of code did not execute:
Destroy(other.gameObject);
Application.LoadLevel (0);
I guess that FixedUpdate() execute and then run OnTriggerEnter. After that, it re-execute FixedUpdate(). So that the cylinder keeps on moving and back and moving.
Now, I changed the code to let character detect the hit of cylinder as I think OnTriggerEnter does not work in my case.
Could anyone help on this thread:
http://forum.unity3d.com/viewtopic.php?p=340954#340954
Thank you all experts here.