- Home /
The question is answered, right answer was accepted
duplicated object destroyed when original object destroyed
hi...i am new to unity...i have a banana prefab tagged as "ban". now have a problem with collider that is a trigger. i written a score system program on it and it works good.but, the problem is that i duplicated the object which has same tag.when i collide with my player the original of the banana prefab increases the score..but not duplicated prefabs increasing it.
i am using this script on that prefab :
public void OnTriggerEnter(Collider hit)
{
if (hit.gameObject.CompareTag("player"))
{
sc += 5.0f;
score_value.text = ((int)sc).ToString ();
Destroy (this.gameObject);
}
}
Is there any solution for this? (please..it's urgent :( )!!!
sorry..that i am not well knowledged person in the way of speaking English and Program$$anonymous$$g.
Answer by meat5000 · Mar 28, 2017 at 05:06 PM
You check for Player tag on hit object but then proceed to destroy the GO attached to 'this' which refers to the script you are reading. So the GO the script is on is destroyed.
Try hit.gameObject instead of this.gameObject if thats what you are trying to achieve.
For all objects to update the same scoreboard you need to grab the actual scoreboard with GetComponent to access the same information.
@meat5000 :) Oh....Ok.....it's working now...i made empty object as child of my player. and made that same script as you said..Thanks $$anonymous$$an.... :)
If the answer is correct, please accept it with the checkmark button near the answer's score.
Thank you. Yeah, That worked for me, it also in the Unity documentation too - later I found it there
https://docs.unity3d.com/ScriptReference/Component.CompareTag.html
// Immediate death trigger.
// Destroys any colliders that enter the trigger, if they are tagged player.
using UnityEngine;
using System.Collections;
public class ExampleClass : $$anonymous$$onoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
Destroy(other.gameObject);
}
}
}