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
1
Question by abhishekabz004 · Mar 01, 2015 at 12:10 AM · c#instantiatedestroyclone

Why could I delete my clones onlY in the same order of instantiation?

Hi! I am kind of new to Unity.

So, I've been trying to destroy set of clones on mouse click instantiated during my game play. My code for instantiation goes like this.

public class inst : MonoBehaviour { public GameObject ch;

 public Transform[] spawnpoint=new Transform[3];    // Use this for initialization
 void Start () {
     spwn();
 }

 void spwn(){
     for (int i=0; i<4; i++) {
         Vector3 spawnVector = new Vector3 (spawnpoint[i].position.x, spawnpoint[i].position.y, spawnpoint[i].position.z);
         
         Instantiate (ch , spawnVector, spawnpoint[i].rotation);
     } 
 }
 // Update is called once per frame
 void Update () {
 
 }

}

And, I have a separate code for destroying the clones on Mouse click which is added to the respective prefab.

But when I am trying to destroy them, they are getting destroyed in the same order in which they are created.

For eg., if I am clicking on my clone in spawnpoint[2] transform, spawnpoint[0] clone is getting destroyed. And on a second click of the same spawnpoint[1[]'s transform is being destroyed.

Please help me in making my code work properly. Thanks in advance :)

p.s.: I might have misunderstood and would have something wrong. I am just a beginner. In that case please correct me.

Comment
Add comment · Show 2
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 meat5000 ♦ · Feb 28, 2015 at 04:01 PM 1
Share

You've posted the wrong code here dude. We need to see the code which doesn't work.

I suspect you are incrementing an integer on each destroy or something to that effect.

avatar image abhishekabz004 · Feb 28, 2015 at 05:36 PM 0
Share

meat5000

Thanks for replying dude. Btw my code is working. But its working in a wrong way

Anyway, this is my code to destroy the clones

 public class onmouse : $$anonymous$$onoBehaviour {
 
     
     // Update is called once per frame
     void Update () {
         if(Input.Get$$anonymous$$ouseButtonDown(0))/
             
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             
             
             
             if(Physics.Raycast(ray, out hit, 10f))
                 Debug.DrawRay (ray.origin, hit.point);
             
             Destroy(GameObject.Find(hit.collider.gameObject.name));
         }
     }
 
 
     
 }
 

Thanks for the help in advance :)

2 Replies

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

Answer by CorruptedTNC · Mar 04, 2015 at 07:19 PM

Encapsulating properly shouldn't have actually fixed the problem (although it was definitely needed for readability). When you instantiate a GameObject from a prefab they all have the same name, which is their prefab name + "(Clone)". This is to help identify the ones you've spawned in the inspector and so on. When you use GameObject.Find it makes a list of all the gameObjects with that name and then picks the first on the list, which I believe is whichever one is first in the inspector. Since Destroy can only destroy 1 object at a time, it will always destroy the first object it finds with that name. The code you posted originally would have worked just fine, but with a small change.

 public class onmouse : MonoBehaviour 
 {
     void Update () 
     {
         if(Input.GetMouseButtonDown(0))  
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if(Physics.Raycast(ray, out hit, 10f))
             {
                 Debug.DrawRay (ray.origin, hit.point);
                 Destroy(hit.transform.gameObject);
             }
         }
     }
 }

hit.collider.gameObject should also have worked. The only problem with what you wrote was that you weren't specific enough with which GameObject you wanted to destroy.

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 meat5000 · Feb 28, 2015 at 08:00 PM

First, try extending your if statement brackets to encapsulate the Destroy routine.

 if(mousebutton)
 {
     ray and hit setup
     if(raycast)
     {
         Debug
         Destroy
     }
 }

Comment
Add comment · Show 2 · 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 abhishekabz004 · Mar 03, 2015 at 06:53 PM 0
Share

thanks for the help bro!! cheers :)

I have another doubt. pls help me understand this one. So I also tried to destroy my gameObject using On$$anonymous$$ouseDown() method. To my surprise the thing worked despite being a 2 code of just 2 lines.

So what is the difference between this On$$anonymous$$ouseDown() method and Physics.raycast() method.?

avatar image meat5000 ♦ · Mar 03, 2015 at 07:25 PM 0
Share

Quite a lot really :)

Rays can be used for all kinds of things within the physics system. On$$anonymous$$ouseDown can be more of a direct interface between an object and a mouse.

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

Destroy a specific instantiated clone? 2 Answers

Cloning Objects with Instantiate() - variables/references for added Components not stored? 3 Answers

Is there anyway to delete clones that are local variables? 1 Answer

[Solved]Instantiated item won't get destroyed until "picked up" again. 1 Answer

How can I destroy my Instance without renaming it? 2 Answers


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