- Home /
Object wont kill player
Ok so i have a java script im useing for my codeing and it is sepost to kill the player but it is not im very new to codeing so i was wondering if anyone here can help me with my problem
heres my code im useing this is in java thank you in advance for any help
function OnTriggerEnter(other : Collider){ //this function checks for the collision of the local Collider which is Trigger (i.e. field-like, and not rigid, objects can pass through the field, but will trigger the function)
if(other.gameObject.name == "Player"){ //the function took an argument "other", it refers to a collider that is passing through the trigger field
GameObject.Destory(other.gameObject); //using our keyword that refers to that external collider, it picks the whole gameObject that somehow relates to that collider, that is, holds it. We than destroy that gameObject
for(var child : Transform in transform.gameObject){ //
GameObject.Destroy(child.gameObject); //those three lines are only needed if you have children of the player objet that have to be removed
} //otherwise, you can remove those 3 lines
Debug.Log("Player was brutalized"); //write a message in a console that the player has r.i.p'ed
}
}
First, add a debug message or break point in this function to make sure this function is excuted by Unity. And i also checked this: If obj is a GameObject it will destroy the GameObject, all its components and all transform children of the GameObject. Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering. That means you dont need to destroy the child. And the time to destroy is after the update. Above all, I'm really doubt if OnTriggerEnter is called.
Answer by NoahConstable · Mar 05, 2014 at 04:17 AM
Hello Max, I assume you were trying to get the Player and its children objects to be destroyed if the Player hit the "kill" object. If this is true, then you really added too much code. I've modified yours to make it simpler and functional as well.
Here is the new code (put it on the "Kill" Trigger object):
function OnTriggerEnter(other : Collider){
if(other.gameObject.name == "Player"){
GameObject.Destroy(other.gameObject);
Debug.Log ("Player was brutalized");
}
}
Also, remember to tag your player as "Player".
Happy Coding!
Noah.
I've decided to elaborate my script in the comment section
//If our "$$anonymous$$ill" trigger object hits an object
function OnTriggerEnter(other : Collider){
//If that object is tagged as "Player"
if(other.gameObject.name == "Player"){
//Destroy the player and all of its children
GameObject.Destroy(other.gameObject);
//Print in the Debug Log, "Player was brutalized"
Debug.Log ("Player was brutalized");
}
}
If you need help with anything else, just ask. If this fixed your problem, please mark your problem as solved, and mark my answer as correct. That way others who come here will know it is correct.
Thank you your awenser helped me alot it now works thanks again :D
im haveing another problem im posting it now if you have time it would be nice if you can help
Answer by cartersupergames · Sep 12, 2021 at 08:36 PM
@maxjadin just use this script: OnCollision Destroy(GameObject Player)
or use OnCollision Teleport(GameObject Player) 65.67465 74.73556 875.8736567
the random numbers are going to be the X, Y, and Z positions
Your answer

Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
RPC kill counter 1 Answer
TimeScale = 0 dosent work with this scirpt. 1 Answer
touch position 2 Answers