x


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);

    }  

}
more ▼

asked May 06 '12 at 12:07 AM

Confused gravatar image

Confused
41 8 10 13

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

@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
more ▼

answered May 06 '12 at 12:54 AM

hijinxbassist gravatar image

hijinxbassist
2k 23 31 38

THANK YOU. works like a charm. also thanks for the tips on simplifying the script and getting rid of unnecessary text.

May 06 '12 at 01:45 AM Confused
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1256
x764
x323
x25

asked: May 06 '12 at 12:07 AM

Seen: 400 times

Last Updated: May 06 '12 at 01:45 AM