- Home /
Tagged Clones not destroyed by script
My script lets me take over another object(possessable) by destroying the first person prefab (spirit) and switching the new object's controls on. I can "unpossess" an object by instantiating a prefab of the spirit and disabling the possessed object. This is repeatable.
BUT, after the first time the "spirit" is destroyed, and I possess then possess and unpossess the object again, there are clones of the "spirit" running around. Despite being tagged as spirit, they will not be destroyed.
How can I destroy the prefab clones? (preferably by still using the tag "spirit").
This code is attached to the possessable objects:
//FOR IT TO WORK:
//Possessable item has this script
//Possessable item has a trigger
//Spirit Gameobject is tagged as "Spirit" (as well as children?)
//Disable Camera and FPSInput Controller script
private var insideSpiritTrigger: boolean = true;
var spiritPrefab : GameObject;
var PossessableCamera : GameObject;
var PossessableInput : FPSInputController;
insideSpiritTrigger = false;
//Define the Spirit
var Spirit : GameObject[];
var SpiritPrefab : GameObject[];
SpiritPrefab = GameObject.FindGameObjectsWithTag("Spirit");
Spirit = GameObject.FindGameObjectsWithTag("Spirit");
private var spirit : GameObject;
function Start(){
spirit = GameObject.Find("Spirit");
}
//If in trigger, then "insideSpiritTrigger" is true
function OnTriggerEnter (spirit : Collider){
if(spirit.tag=="Spirit")
insideSpiritTrigger = true;
}
function OnTriggerExit (spirit : Collider){
if(spirit.tag=="Spirit")
insideSpiritTrigger = false;
}
function Update() {
//when trigger is true and E is pressed, destroy spirit
if ( (insideSpiritTrigger == true) && (Input.GetKeyDown(KeyCode.E)) ) {
//destroy spirit
for (var spirit in Spirit){
Destroy(spirit);
}
//activate possessable
PossessableInput.enabled = true;
PossessableCamera.active = true;
}
//if possessed
if ( (PossessableInput.enabled == true) && (Input.GetKeyDown(KeyCode.Q)) ) {
//deactivate possessable
PossessableInput.enabled = false;
PossessableCamera.active = false;
//bring back Spirit
var createSpirit : GameObject = Instantiate(spiritPrefab, transform.position + Vector3(0,5,-3), transform.rotation);
}
}
Answer by hijinxbassist · May 06, 2012 at 12:54 AM
@Confused You dont need to say
if(insideSpiritTrigger == true)
just say
if(insideSpiritTrigger )//If boolean is true
for a false boolean statement you would say
if(!insideSpiritTrigger )//If boolean is false
So what you want to do is pick up 1 spirit and destroy all of them once one has been picked up? Thats how the current script reads. If you want to destroy the same one you pickup, i would rephrase the code as such
function OnTriggerStay (spirit : Collider)
{
if(spirit.tag=="Spirit")
{
if(Input.GetKeyDown(KeyCode.E))
{
//Do something with the spirit before destroying?
Destroy(spirit.gameObject);
}
}
}
But this method turns the trigger function into an update-type function. So your current method is more efficient, just add another var for your collision "spirit" object
var curSpirit:GameObject;
function OnTriggerEnter(spirit:Collider)
{
if(spirit.tag=="Spirit")
{
insideSpiritTrigger=true;
curSpirit=spirit.gameObject;
}
}
In Update..
if(insideSpiritTrigger && Input.GetKeyDown(KeyCode.E))
{
//Do something with the spirit?
Destroy(curSpirit);
}
Q/"//Spirit Gameobject is tagged as "Spirit" (as well as children?)" /A/ No dont tag the children.
NOTE If you need a way to keep track of lots of these spirit object and be able to tell them apart, consider creating an ID script and adding it to the prefab and increasing the ID number as you instantiate them(or set them in the inspector as 0,1,2,3,4...)
//ID SCRIPT
var id:int;
Thats it. Then you can say something like
if(spiritObjcet.GetComponent(ID).id==curID)//current ID variable matches the found spirits ID
THAN$$anonymous$$ YOU. works like a charm. also thanks for the tips on simplifying the script and getting rid of unnecessary text.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071136im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trouble destroying a instantiated Prefab? 1 Answer
How to refer to a collision between clones of the same prefab? C# 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Destroying an instantiated prefab particle effect 4 Answers
How to destroy all gameobjects active in hierarchy? 1 Answer