Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Nelawyth · Aug 27, 2015 at 12:35 PM · c#raycastunity5

How do i make a raycast sign text? Answers Appreciated

Hi there, im struggling to find an answer for a script that can display text when i press E. For example:

If i look at a sign and press E i want it to display some text (a string variable using public string SignText;)

All the other answers are to do with doors and stuff i dont care about. Please help :) All answers are greatly appreciated -Nelawyth

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by CarbonNanoRob · Aug 27, 2015 at 01:36 PM

This is how I have done mine, which sounds like its similar to what you're looking for. Attach this to your Camera, and make sure you have a canvas and a text component and set the tags correctly, or just tweak it to your preferences. And add this to the top (without quotation marks) "using UnityEngine.UI;"

 {
         public float InteractDistance = 2.75f;
         Text HighlitedName;
         RaycastHit Hit;
         void Start()
         {
             HighlitedName = GameObject.Find("CanvasText").GetComponent<Text>();
         }
         void Update()
         {
             HighlitedName.text = null;
             if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward),out Hit, InteractDistance))
             {
                 if (Hit.collider.tag.Equals("Interactable"))
                 {
                     HighlitedName.text = (Hit.collider.name);
                 }
             }
         }
     }
Comment
Add comment · Show 3 · 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 Nelawyth · Aug 27, 2015 at 02:30 PM 1
Share

@CarbonNanoRob Thanks for the support, but i dont quite know how to use the code, Nothing happens when i Press E to the sign, Also I just get a NullReferenceException

alt text

(+2 rep) -Nelawyth

nullreference.png (311.5 kB)
avatar image CarbonNanoRob · Aug 27, 2015 at 02:45 PM 0
Share

okay, lets see.

  • $$anonymous$$ake sure there is a canvas in the scene(GameObject->UI->Canvas).

  • Create a text object on the canvas(GameObject->UI->Text).

  • Name the text object to "CanvasText".

  • If you don't have a tag created called "Interactable", Create one and change your signs tag to It.

  • Add this to the If(Hit.collider.tag.equals("Interactable")

                          && Input.Get$$anonymous$$eyDown($$anonymous$$eycode.E))
    

and be sure that you have "using UnityEngine.UI;" at the top of your script.

avatar image TheRealSam1994 · Jun 11, 2021 at 09:41 AM 0
Share

Hey! Im also pretty new to Unity, and have tried to use your script but i keep getting NullReferenceExceptions. Im using textmeshpro so have changed the Text HighlitedName to TextMeshProUGUI. Ive set the name of my text to CanvasText and have made my objects tag "Interactable", do you have any advice for why it is not working?

avatar image
-1

Answer by Nelawyth · Aug 27, 2015 at 05:57 PM

@CarbonNanoRob Could you fix the code so i can use the public string SignText; to display text, Im having trouble modifying the code

-Nelawyth

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

why Raycast2D doesn't work properly? 0 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Raycast2D is inconsistent 0 Answers

2 raycasts on the fpwalker/camera 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