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 bloodair · May 12, 2011 at 12:53 PM · instantiateraycastobjectdestroyhit

Destroying object using his name and raycast

I have a big problem its driving me crazy so im hoping someone will help me :D

I have this game when i press mouse button 1 it instantiates a cube at the position when the ray hits. This part of the program works fine and looks like that :

if(Input.GetButtonDown("Fire1")&& Physics.Raycast (cam.position, cam.forward, hit, 100)!=false )

{

Instantiate(box,point,Quaternion.identity);

}

Then when i press mouse button 2 the object that the ray hits is destroyed. That part works allso well, too well and it looks like this:

if(Input.GetButtonDown("Fire2"))

{ if (Physics.Raycast (cam.position, cam.forward, hit, 100) ) {

     if(hit.transform.gameObject)
     {

     Destroy(hit.transform.gameObject);

     }
 }

}

The problem is when i press mouse button 2 it destroys every object the raycast hits like for example the terrain and i dont want that i want it to destroy only the box and not eany other object and i allso dont want to turn on the Ignore Raycast on eny of my objects.

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 Jessy · May 12, 2011 at 12:59 PM 0
Share

http://unity3d.com/support/documentation/Components/Layers.html

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by GesterX · May 12, 2011 at 12:59 PM

Just check what object you're hitting. For example you could tag your cube as "cube" or "destroyable".

if(hit.transform.gameObject && hit.transform.gameObject.tag == "Cube")
{
Destroy(hit.transform.gameObject);
}
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 Joshua · May 12, 2011 at 01:22 PM 0
Share

Yep. Alternatively use gameObject.name.Contains("Cube") to not have to add in a ta :)

avatar image MoizSafdar · Apr 25, 2016 at 08:48 PM 0
Share

@GesterX and Xornogard Answer No1: i used your type of logic but i have 1 problem in this code i want same as your code if i click on the game object so i destroy but sometime it doesn't work . my game become static (just like this, no game object destroy when i clicked) And Answer No 2 : how to destroy gameobject horizontally ?

avatar image
0

Answer by Xornogard · Jan 28, 2015 at 11:12 AM

Raycasthit returns you an information that you've hitted an object, If you want to destroy a certain object, then you have to tag it and add

 if(hit.transform.gameObject && hit.transform.gameObject.tag == "objectTagName")
 {
 Destroy(hit.transform.gameObject);
 }

otherwise it will destroy anything it hits ;)

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

2 People are following this question.

avatar image avatar image

Related Questions

Raycast Specific Object/Instantiate Explosion 1 Answer

how to raycast instantiated game characters 1 Answer

How to Destroy a GameObject when the player looks away 1 Answer

Instantiating droppable items on destroy game object 1 Answer

Cannot destroy child object in prefab- Error 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