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 anwserman · Apr 04, 2012 at 10:02 AM · collisiongameobjectraycast

Raycast Collision - Getting Custom Values from GameObject

So, I think this could be an extension of this question:

http://answers.unity3d.com/questions/141529/getting-gameobject-from-raycast-hit.html

Anyway, using Raycasts, I need to get the following information from the GameObject:

*Is a vehicle (simple boolean value)
*What type of sound to play when walking on (GameObject possibly, but grass, concrete, metal, etc.)

I'd prefer to not have to give values for each GameObject in the map. If a value isn't found, assume False for the first set and nothing for the second. Any suggestions on how I can go about doing this?

Thank you much!

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

Answer by aldonaletto · Apr 04, 2012 at 11:51 AM

You can create a simple script and attach it to the game objects of interest, then set the variables in the Inspector. When hitting the object, get this script and read its variables.
The script attached to the objects could be something as short as this (let's call it Variables.js):

enum Mat {Nothing, Metal, Grass, Concrete, Stone}; // possible materials

var vehicle: boolean = false; // change this in the Inspector, if necessary var myMat: Mat = Mat.Nothing; // select the material in the Inspector To get these values from a hit object, do the following:

  ...
  if (Input.GetMouseButtonDown(0)){
    var hit: RaycastHit;
    var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, hit)){   // if some object hit...
      // try to get its Variables script:
      var values: Variables = hit.transform.GetComponent(Variables);
      if (values){   // if the object has the script Variables attached...
        var isVehicle: boolean = values.vehicle;   // read the variables
        var mat: Mat = values.myMat;
      } else {  // but if it doesn't have the script...
        isVehicle = false;  // assume the default values
        mat = Mat.Nothing;
      }
    }
  }
  ...
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 anwserman · Apr 05, 2012 at 04:48 AM 0
Share

Thank you! I'll be sure to test this out soon

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

Grabbing MonoBehavior object from collision 1 Answer

Send Message On Collision 1 Answer

Layered Collisions 1 Answer

Select a group of gameobjects that are contiguous 1 Answer

Multi object detection with single ray cast 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