• 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
Question by bdonges · Mar 12, 2017 at 03:18 PM · messages

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.

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

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.

Comment
Zondi

People who like this

1 Show 1 · 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 roman_sedition · Mar 13, 2017 at 12:53 PM 0
Share

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.

Make OnMouseOver() a virtual method, and then make override methods for OnMouseOver() 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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