- Home /
mouse input - displays object description on screen
Hello. Posted this on the forums but I think I should have done it here, I'm looking for a simple script that on Fire1 click, will display text on screen with the description of the object that has been clicked on. I'm very new to Unity and even more with scripting and languages, I have googled a lot and have a slight idea of what's going on behind some scripts, but not sure how to put this one together? Thank you for any help out there.
As your question currently stands, it is not a good fit for Unity Answers. The primary problem is, "I'm looking for a simple script..." which can be interpreted to mean that you are looking for someone to write a script for you. Typically questions that ask for scripts are ignored or closed. Beyond that problem, your questions lacks the details necessary to do more than guess at the correct solution. $$anonymous$$y suggestion is for you to research the following:
How to detect a click on a game object:
Three ways to display text (and there are more):
Answer by clunk47 · Sep 24, 2013 at 04:17 AM
I'd attach a script to each object and for example's sake, name it "Description". Here's a simple C# example on how you would go about using Raycast from the main camera using mousePosition, then using GetMouseButtonDown to bring up the string as a GUI Label. Also take a look at ScreenPointToRay.
using UnityEngine;
using System.Collections;
public class Description : MonoBehaviour
{
public string label = "Change this in the inspector for each object";
Ray ray;
RaycastHit hit;
bool showLabel = false;
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, Mathf.Infinity) && Input.GetMouseButtonDown(0))
{
if(hit.collider == this.collider)
{
showLabel = true;
}
else
showLabel = false;
}
}
void OnGUI()
{
if(showLabel)
GUI.Label(new Rect(0, 0, Screen.width, 256), label);
}
}
@LordSoth30, you now have enough karma to upvote (thumbs up) comments and answers, you should do so for @robertbu's comment, as it is helpful advice. You can vote up my answer if you feel like it too lol
Thank you very much for the example, that's brilliant. I have read through the Unity wiki & script references, it was just putting it all together that I was having trouble with. Thank you once again!
Always happy to be of help, always feel welcome to come back if you need help with anything else :)