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 SYRSA · Jun 12, 2014 at 02:22 AM · raycaststringname

How do I get string name from a script on a rayhit gameobject?

I have a script that raycasts an area infront of a tiny ship that reads molecules(gameobjects).

I have made 2 script for this, but I don't know how to make the first script get the string from the script(MoleculeNametag.js) of the gameobject it's hitting.

--(MoleculeNameDisplayer.js)---------------------

 #pragma strict
 
 static var Name : String;
 
 function Update()
 {
     Debug.DrawRay(transform.position, transform.forward, Color.red, 10);
     var hit : RaycastHit;
     if (Physics.Raycast(transform.position, transform.forward, hit, 10)){
     
     Name = hit.collider.gameObject.name; <----(This is what I don't know what to do with)
     }
 }



--(MoleculeNametag.js)---------------------

 #pragma strict
 
 var NameOfMolecule : String;
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 SYRSA · Jun 12, 2014 at 04:24 AM 0
Share

Does anyone know how I can achieve this? Or am I not explaining my problem clearly?

1 Reply

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

Answer by Huacanacha · Jun 12, 2014 at 04:55 AM

Here it is in C#:

 MoleculeNametag molecule = hit.collider.gameObject.GetComponent<MoleculeNametag>();
 if (molecule != null) {
     Name = molecule.NameOfMolecule;
 }

And here is the docs reference for GetComponent which has JS/Unityscript examples. Looks like it would be "gameObject.GetComponent(MoleculeNametag)" in JS;

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 SYRSA · Jun 12, 2014 at 05:25 AM 0
Share

Thank you. I didn't realize how obvious it was.

avatar image Huacanacha · Jun 12, 2014 at 10:46 PM 0
Share

No problem ;)

Note that if you have other colliders in your scene you can use Layers to ensure you only ever 'hit' a molecule. To do this create a new physics collision layer, then use a Raycast method that accepts a Layer$$anonymous$$ask... Raycast will then only return a hit on the layers you specify. Layer$$anonymous$$asks are easy to setup via the inspector (i.e. if you have a public Layer$$anonymous$$ask field on your script) but a little painful to do via code.

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

22 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

Related Questions

Displaying name of currently focused object. 3 Answers

Raycasts ignoring colliders based on Collider Name 2 Answers

Get object name from raycast. 1 Answer

Show variable name from a class as String 2 Answers

How do I retrieve Mecanim State Name? 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