Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by frdrk · May 30, 2011 at 07:35 PM · instantiatedestroyclone

Destroy a specific instantiated clone?

Hi!

The player in my game can walk around in the world and with a mouse click place objects in the world. The objects placed are instantiated from a prefab and are called "Brick(Clone)" in the hierarchy. The code for the instantiate below.

var cube : Transform;

function setcube () { // Get aim position and round to nearest grid space var aimx = Mathf.RoundToInt(transform.position.x + 0); var aimy = Mathf.RoundToInt(transform.position.y + 0); var aimz = Mathf.RoundToInt(transform.position.z + 0);

 // Place cube
 var cube = Instantiate(cube, Vector3 (aimx, aimy, aimz), Quaternion.identity);

}

It seems to work pretty ok. But then I want the player to be able to remove an object with a right click. I've been at this for a few days but can't think of a way to to it. How do I know which specific instance of a clone to destroy? Destroy(gameObject) just destroys the first created (there could be thousands). I've messed around with OnCollisionEnter functions on the cube to detect when a user interacts with them but it won't work. The clones don't seem to have IDs or any way of getting to a specific one the player clicks on.

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

2 Replies

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

Answer by almo · May 30, 2011 at 08:03 PM

Found this in another Unity Answer, just altered it a teeny bit. Searching is good. :)

 function Update()
 {
     if (Input.GetMouseButtonDown(1))
     {
         var hit : RaycastHit;
         if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
         {
             Destroy(hit.collider);
         }
     }
 }
Comment
Add comment · Show 6 · 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 frdrk · May 30, 2011 at 09:41 PM 0
Share

Ahh, never had the brains to look at Raycasts. Been searching like crazy.

It does work in a way. But i've found two issues that i don't know how to get around.

Firstly the objects i'm trying to destroy don't have rigidbody so it looks like all it does is remove the actual collider from the cube, not the cube itself. (Strangely it will remove the plane that makes up the floor without it having a rigidbody).

Secondly i need to make sure the cubes are all the player can destroy - not other stuff. I stuck it in another if - but it won't recognize the cubes by tag?

avatar image ina · May 30, 2011 at 09:42 PM 0
Share

try a substring name compare, if it starts with Cube, then destroyable, otherwise not.

avatar image almo · May 31, 2011 at 01:29 PM 0
Share

You can check hit.collider.tag if you want to check tags. $$anonymous$$aybe Destroy(hit.collider.GameObject) would destroy the whole thing? GameObject referrs to the GameObject the collider is attached to.

avatar image frdrk · May 31, 2011 at 08:30 PM 0
Share

Thanks guys! Finally got it working with this code:

 // Deletes stuff from the world
 if (Input.Get$$anonymous$$ouseButtonDown(1))
 {
     var hit : RaycastHit;
     if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
     {
         if(hit.collider.gameObject.tag == "Cube"){
             Destroy(hit.collider.gameObject);
         }
     }
 }
avatar image SirGive · May 31, 2011 at 08:32 PM 0
Share

why are you using a ray cast AND get mouse button down? Just attach the "on$$anonymous$$ouseDown" function to the prefab and use Destroy(this).

Show more comments
avatar image
0

Answer by SirGive · May 31, 2011 at 08:34 PM

This would work better.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Pass a copy of a GameObject as variable to another script? 1 Answer

How to delete instantiated GameObject 4 Answers

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

Instantiate and Destroy an object while crossing a line with its clone also 1 Answer

Why could I delete my clones onlY in the same order of instantiation? 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