- Home /
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
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);
}
}
}
}
@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
(+2 rep) -Nelawyth
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.
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?
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
Your answer
Follow this Question
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