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 devcutters · Jun 11, 2014 at 06:34 AM · raycasttagtags

find tag from raycast

hi everybody ive been trying to make an if statement so when i click the block it get destroy depending on the item equipped and the item your trying to mine i tried different ways i have problems identifying the tag for the times heres the scripts

if(Input.GetMouseButtonDown(0 && WoodShovel.ShovelSelected == true) || Input.GetMouseButtonDown(1)) { var Hit : RaycastHit; var LookingDirection = transform.TransformDirection(Vector3.forward); if (Physics.Raycast(transform.position,LookingDirection, Hit, Range)) { if(Input.GetMouseButtonDown(1)) { if (BlockSelected == 1) { var GrassBlock : Transform = Instantiate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalized, Quaternion.identity); GrassBlock.tag = "GrassBlock"; } if (BlockSelected == 2) { var StoneBlock : Transform = Instantiate(StoneBlock, Hit.collider.transform.position + Hit.normal.normalized, Quaternion.identity); StoneBlock.tag = "StoneBlock"; } } else { if(BlockSelected == 0) { if ( Hit.transform.tag == "GrassBlock"){ Destroy(Hit.transform.gameObject,WoodShovelDestroyTime); } else { Destroy(Hit.transform.gameObject,12); } } if(BlockSelected == 1) { Destroy(Hit.transform.gameObject,GrassBlockDestroyTime); } if(BlockSelected == 2) { Destroy(Hit.transform.gameObject,StoneBlockDestroyTime); } } } }

Comment
Add comment · Show 3
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 Nick4 · Jun 11, 2014 at 08:10 AM 0
Share

Please reformat your code, it's quite hard to read this way.

avatar image robertbu · Jun 11, 2014 at 03:13 PM 0
Share

On lines 25, 30, 36 and 40 you are accessing either a stale or an undefined Hit. You only know that Hit is value inside the if statement starting at line 1.

avatar image devcutters · Jun 11, 2014 at 10:35 PM 0
Share

srry for the formating i tried to use the code sample but in the end it all fucked the formating ill try to make it nicer next time thanks for comment

1 Reply

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

Answer by pumpkinszwan · Jun 11, 2014 at 11:15 AM

Try the 3D equivalent of this:

 // store the position of the mouse click
 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 //Cast a ray to detect object clicked
 Raycast2D  hitObject = Physics2D.Raycast(mousePosition, new Vector2(0, 0), 0.1f, mask);

 //can now access the tag and/name from the hitObject's transform
 string objectTag = hitObject.transform.tag;
Comment
Add comment · Show 1 · 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 devcutters · Jun 11, 2014 at 10:34 PM 0
Share

thanks much thats exactly what i was looking for i will give it a try thank you :)

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

23 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 avatar image avatar image avatar image avatar image

Related Questions

Raycast isn't working when with tag 2 Answers

Object not surviving on load? 1 Answer

Swapping a tag for string not working, replacing a tag with a public string, help. 1 Answer

How do you change the position of a Raycast? 1 Answer

Raycast detection / lag issue 0 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