- Home /
How to "Stop my character from dying for no reason"?
Hi, i recently figured out a way to make my character die if hit on the head by a falling object that needs to be "not trigger" to prevent it from falling through the ground.
Unfortunately, my character, just sometimes, dies for no apparent reason (or for a reason i can't recognice) and i guess i made some mistake with this script, have a look!
function Start () {
}
function Update () {
}
function OnTriggerEnter (otherObject: Collider) {
if((GameObject.Find("CubePrefab(Clone)")) ){
Destroy(GameObject.Find("First Person Controller"));
I'm completely a newbie with scripting so don't go mad if you read something that is just stupid to you ^^
I noticed that if i place the empty object this script is on, right over my character, he istantly dies, so i moved it a bit far, not too much, and i guess this could give you some kind of help with helping me solve the problem :)
So, what can i do to solve my problem?
Thanks everyone :D
Answer by raimon.massanet · Jan 08, 2014 at 03:38 PM
You are destroying your player if it collides with anything and there is a "CubePrefab(Clone)" object in the scene. I don't think that's what you intend to do.
Instead, you could assign a tag to the falling object and then:
function OnTriggerEnter (otherObject: Collider)
{
if(otherObject.gameObject.tag == "FallingObject" )
{
Destroy(GameObject.Find("First Person Controller"));
}
}
On the other hand, GameObject.Find
is a very expensive function that should be avoided. Declare a public GameObject
variable and assign the first person controller game object from the inspector.
Thanks mate! This totally worked out for my little character, that is now grateful with you, kind person :3 I realized the point of .Find when i found out that noone was using that "system" and i'm trying to think on how to make that work differently, but that's not my main problem atm, will be as soon as i'll have solved many other problems ahahah ^^
Answer by tanoshimi · Jan 08, 2014 at 03:34 PM
I'm going to add some comments to your code to help you understand the logic of what's going on...:
// This function gets called every time the object to which this script is attached
// enters any trigger
function OnTriggerEnter (otherObject: Collider) {
// If there is any object anywhere is the scene called "CubePrefab(Clone)"
if((GameObject.Find("CubePrefab(Clone)")) ){
// Find and destroy the object called "First Person Controller"
Destroy(GameObject.Find("First Person Controller"));
}
}
... is that really what you meant?
Well, your answer pretty much clarified me the point! So thank you ^^ Unluckily raimon gave me the thing that made me solve the issue and so i gave him the "right answer", sorry bro :D I wish i could accept both of your answers :(