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 Timseinelps · Nov 20, 2013 at 08:29 PM · raycastdetectionminecraft

need Block Placer/Eraser to not destroy any other gameobjects

As I am a noob to Unity I need some help. I tried to make a game with block placing/erasing and used this script:

 var blockLayer : LayerMask = 1;
 var range : float = Mathf.Infinity;
 var hit : RaycastHit;
  
 function Update () {
     if (Input.GetMouseButtonDown(0))
         Build();
     if (Input.GetMouseButtonDown(1))
         Erase();
 }
  
 function Build() {
     if (HitBlock()) {
         var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
         cube.transform.position = hit.transform.position + hit.normal;
     }
 }
  
 function Erase() {
     if (HitBlock())
        
         Destroy(hit.transform.gameObject);
 }
  
 function HitBlock() : boolean {
     return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
 }

The problem is that the script is also destroying my terrain and any other gameobject. I want to somehow detect if the object that is hit by the ray is a cube that is allowed to be destroyed or a object that shouldn't be destroyed.

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
0
Best Answer

Answer by GameVortex · Nov 20, 2013 at 10:23 PM

There are many ways of achieving this. One of the simplest ways that does not require extra scripts could be to just name the object something specific when you create it. That way you can check if the gameObect the raycast hit has the same name or not and then destroy it if it has.

Example:

 function Build() {
     if (HitBlock()) {
         var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
         cube.transform.position = hit.transform.position + hit.normal;
         cube.name = "ErasableCube";
     }
 }
  
 function Erase() {
     if (HitBlock())
         if(hit.name == "EraseableCube") {
             Destroy(hit.transform.gameObject);
         }
 }

You could also do this using the "Tag" property of gameObjects: Tag. You will need to create the tags in the tagmanager first though.

Comment
Add comment · Show 10 · 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 Timseinelps · Nov 21, 2013 at 04:09 PM 0
Share

Thx for the quick help! :)

avatar image Timseinelps · Nov 21, 2013 at 04:30 PM 0
Share

But there seems to be a problem when I want to right-click I get this error:

$$anonymous$$issingFieldException: UnityEngine.RaycastHit.name Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.FindExtension (IEnumerable`1 candidates) Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.Create (SetOrGet gos) Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.CreateGetter () Boo.Lang.Runtime.RuntimeServices.DoCreatePropGetDispatcher (System.Object target, System.Type type, System.String name) Boo.Lang.Runtime.RuntimeServices.CreatePropGetDispatcher (System.Object target, System.String name) Boo.Lang.Runtime.RuntimeServices+c_AnonStorey16.<>m_A () Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.Dispatcher$$anonymous$$ey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)

would be nice if could get help again.

avatar image GameVortex · Nov 21, 2013 at 05:10 PM 0
Share

Aah, sorry about that. $$anonymous$$y bad, it should be:

 If(hit.transform.name == "EraseableCube")
avatar image Arklight · Nov 21, 2013 at 05:15 PM 0
Share

Lowercase "if" though ;)

avatar image GameVortex · Nov 21, 2013 at 05:34 PM 0
Share

@Arklight Of course. =) autocorrect was on. =P

Show more comments

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast hit ignores everything except for the terrain 1 Answer

Raycast not detecting a hit from far away, it only detects when close. 0 Answers

Raycast on an AI Game object 1 Answer

How to find the portion of a 3D object that is exposed from a specific angle? 1 Answer

Raycast and actions. 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