Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Confused · May 06, 2012 at 12:07 AM · prefabdestroytagclones

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);
     
     }    
         
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Confused · May 06, 2012 at 01:45 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges