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 Sek19 · Dec 11, 2014 at 12:39 PM · destroyinstanceclone

Destroy instance clone of gameobject

I am creating a small interaction where you click on a table and a small animation plays in response. What I'm getting caught up on is that when I instantiate the click I cannot rid the click object from the game. This has ended up with me bogging it down with too many objects in scene. if you guys could take a look at my code I would greatly appreciate it! As I have been looking to the answer to this problem for hours now haha!

the fist block of code is in java script and the second is in c#

 #pragma strict
 
 var touch : GameObject;
 
 function OnCollisionEnter (other : Collision) {
 //public float initializationTime;
 
 if (other.transform != transform && other.contacts.length!=0)
 {
     for (var i = 0; i < other.contacts.length; i++)
      {
     Instantiate(touch, other.contacts[i]. point,Quaternion.identity);
     
     Destroy(GameObject.Find("Click(Clone)"),10);
      }     
      }






 using UnityEngine;  
 using System.Collections;  
 
 public class ImageSequenceTextureArray : MonoBehaviour  
 {  
     //An array of Objects that stores the results of the Resources.LoadAll() method  
     private Object[] objects;  
     //Each returned object is converted to a Texture and stored in this array  
     private Texture[] textures;  
     //With this Material object, a reference to the game object Material can be stored  
     private Material goMaterial;  
     //An integer to advance frames  
     public int frameCounter = 0;     
     
     void Awake()  
     {  
         //Get a reference to the Material of the game object this script is attached to  
         this.goMaterial = this.renderer.material;  
     }  
     
     void Start ()  
     {  
         //Load all textures found on the Sequence folder, that is placed inside the resources folder  
         this.objects = Resources.LoadAll("Sequence", typeof(Texture));  
         
         //Initialize the array of textures with the same size as the objects array  
         this.textures = new Texture[objects.Length];  
         
         //Cast each Object to Texture and store the result inside the Textures array  
         for(int i=0; i < objects.Length;i++)  
         {  
             this.textures[i] = (Texture)this.objects[i];  
         }  
     }  
     
     void Update ()  
     {  
         //Call the 'PlayLoop' method as a coroutine with a 0.04 delay  
         StartCoroutine("Play",0.00f);  
         //Set the material's texture to the current value of the frameCounter variable  
         goMaterial.mainTexture = textures[frameCounter];  
 
 
         
     }  
 
 
     
     //The following methods return a IEnumerator so they can be yielded:  
     //A method to play the animation in a loop  
     IEnumerator PlayLoop(float delay)  
     {  
         //Wait for the time defined at the delay parameter  
         yield return new WaitForSeconds(delay);    
         
         //Advance one frame  
         frameCounter = (++frameCounter)%textures.Length;  
         
 
 
         //Stop this coroutine  
         StopCoroutine("PlayLoop");  
 
     }    
     
     //A method to play the animation just once  
     IEnumerator Play(float delay)  
     {  
         //Wait for the time defined at the delay parameter  
         yield return new WaitForSeconds(delay);    
         
         //If the frame counter isn't at the last frame  
         if(frameCounter < textures.Length-1)  
         {  
             //Advance one frame  
             ++frameCounter;  
             /*
             if(frameCounter >= 400)
             {
                 
                 objects =null;
                 textures = null;
                 goMaterial = null;
                 frameCounter = frameCounter - 400;
 
             } */
         }  
         Destroy(GameObject.Find("Touch"),10);
         //Stop this coroutine  
         StopCoroutine("Play");  
     }   
 
 
 
     
 }  

Comment
Add comment · Show 1
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 Klarax · Dec 11, 2014 at 12:43 PM 0
Share

to click an object use raycast to get the clicked object then its a standard destroy()

2 Replies

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

Answer by Baste · Dec 11, 2014 at 01:31 PM

Instantiate returns the instantiated object, so you can just call Destroy directly on it when you spawn the object:

  Destroy(Instantiate(touch, other.contacts[i]. point,Quaternion.identity), 10);

You're also trying to destroy the gameobject in the Play-coroutine. If you want to destroy it there, reference it directly instead of trying to Find() it:

 IEnumerator Play(float Delay) {
     ...
     Destroy(gameObject);
 }

You're also calling StopCoroutine("Play") at the end of the Play-coroutine. This is redundant - the coroutine will stop when you get to the end of it. The same goes for the PlayLoop routine.

Also, when you're starting the Play-coroutine, you're sending in a delay of 0 seconds, while claiming that you're playing it with .04 seconds in the comment.

Comment
Add comment · 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
0

Answer by Sek19 · Dec 11, 2014 at 05:18 PM

Thank you so much for your help I'll be incorporateing that into the code! Now I do have another question about what you've said. With the play loops, if I were to remove the loop it will play only once, every time it's instantiated, right? I've had some issues where I click, and it only plays once for the first clone and all clones created afterwards it registers touch but stops any animation. It mainly happens once I change the courutiens to play instead of playLoop. Thank you for the quick reply and I hope to hear back from you!

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I properly destroy an instance of a prefab from within its own script? 1 Answer

How to destroy gun bullet clone? 1 Answer

Destroy instantiated object instead of prefab 3 Answers

How to instantiate from prefab, not from instance? 3 Answers

Clones not being Destroyed 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