- Home /
How do you display a message to a player when he clicks on an object?
What I am attempting to do is set up objects so that when a player left mouse clicks on an object it gives a "look at object message(e.g. "A candle" or "You see nothing special.").
Right clicking will give a "Take object message" (e.g. "You take the object." or "You can't take that."). Message would depend on a bool (canTake = true).
Assuming the player can take the object the object is destroyed and added to the player's inventory.
I have tried different ways but nothing seems to work.
Answer by bdonges · Mar 13, 2017 at 10:02 AM
Update: I created a "LookAt" script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LookAt : MonoBehaviour {
public bool canTake = false;
public string lookMsg = "You see nothing special.";
public string takeMsg = "You can't take that.";
public string openMsg = "You can't do that.";
public GameObject messageObject;
string message;
GameObject MessageManager;
private bool showLabel = true;
public void Start()
{
// InvokeRepeating("ToggleLabel", 1,1); InvokeRepeating("ClearMessage", 1, 3); }
//public void ToggleLabel()
//{
// showLabel = !showLabel;
//}
public void ClearMessage()
{
message = "";
}
private void Update()
{
OnMouseDown();
}
void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
message = lookMsg;
Debug.Log("Pressed left click.");
}
if (Input.GetMouseButtonDown(1))
message = takeMsg;
Debug.Log("Pressed right click.");
if (Input.GetMouseButtonDown(2))
message = openMsg;
Debug.Log("Pressed middle click.");
}
void OnGUI()
{
if (showLabel)
{
GUI.Label(new Rect(250, 700, 1000, 1000), "<color=white><size=32>" + message + "</size></color>");
}
}
}
And a "MessageManager" script:
using UnityEngine; using System.Collections; using UnityEngine.UI; // include UI namespace so can reference UI elements using UnityEngine.SceneManagement; // include so we can manipulate SceneManager
public class MessageManager : MonoBehaviour {
// static reference to message manager so can be called from other scripts directly (not just through gameobject component)
public static MessageManager mm;
// game performance
public string pMessage = "Player Message:";
// UI elements to control
public Text UIPlayerMessages;
// set things up here
void Awake()
{
// setup reference to message manager
if (mm == null)
mm = this.GetComponent<MessageManager>();
// setup all the variables, the UI, and provide errors if things not setup properly.
setupDefaults();
}
// setup all the variables, the UI, and provide errors if things not setup properly.
void setupDefaults()
{
// get the UI ready for the game
refreshGUI();
}
void refreshGUI()
{
UIPlayerMessages.text = pMessage;
}
//send messages to the player
public void DoMessage(string message)
{
pMessage = message;
Debug.Log("Got to DoMessage.");
}
}
I attached both to an object. Now, when I left click I get the look message, right click gives the take message and center click gives the open message.
My current issue is that I get the messages anywhere I click in the scene not just on the object.
What I would do is make an abstract class called, ClickableObject, and then make children classes for all the types of objects that you want to be able to interact with.
$$anonymous$$ake On$$anonymous$$ouseOver() a virtual method, and then make override methods for On$$anonymous$$ouseOver() in all the child classes that inherit from ClickableObject.
Then you define how you want your clickable objects to interact with, if a mouse button is pressed when the mouse is over the object.
Your answer
Follow this Question
Related Questions
How to use Application.OpenUrl("sms") for multiple numbers? 2 Answers
Wierd error messages concerning audio play and pause? 1 Answer
How to avoid overflowing network Channels 0 Answers
What should i do in javascript, to be able to place hints during the game 0 Answers
How to display a message from socket (python) on the screen (Unity) 0 Answers