• 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 Iceblitzyt · Oct 19, 2012 at 02:56 PM · c#messagebox

how do i make this message box appear if player is near?

Hey,

I'm fairly new to coding so please don't murder me if this is a pretty basic answer. I tried doing this in c# as shown below.

 using UnityEngine;
 using System.Collections;
 public class mesagebox : MonoBehaviour {
 
     void OnGUI (){
         
         if(transform.position.x <20){
         
         GUI.Box(new Rect(50,50, 500, 500), "Text Area");
             
         
         
         }
     }
 }


I couldn't get it to work. I'm trying to get a message box appear when my player is near the cube. I figured i needed to put in a box collider and trigger it. but it didn't work. I figured i might need to tag the player in some way to trigger it, I spent the better part of an hour looking through google and such for an answer. The only resources i could find, including unity scripting reference was all java script.

Can someone please provide an example?

EDIT: I found a way how to do it in Javascript : http://www.youtube.com/watch?v=IXYTlPw0kyY&feature=relmfu

But i need to know c# as it's the language im trying to learn.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Noah-1 · Oct 20, 2012 at 06:13 PM

Hi Ice, so lets try the next thing:

 using UnityEngine;
 using System.Collections;
 
 public class scriptMono : MonoBehaviour {
     public Transform other;
     
     void Update() {
         if (other) {
             float dist = Vector3.Distance(other.position, transform.position);
              print ("Distance to other: " + dist);
          if(dist < 5 ){Debug.Log ("Player is close, render box now");}
         }
     }
 }

Really simple script, i tested it, if your player is near the cube it will print that message, just modify it calling your GUI function and we´re done. Hope it helps!

Comment

People who like this

0 Show 4 · 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 paulaceccon · Oct 20, 2012 at 06:19 PM 0
Share

Yeah, it's exactly what I'm trying to explain! Sorry, I'm not good to explain things.

avatar image paulaceccon · Oct 20, 2012 at 06:22 PM 0
Share

I just would add a thing, Iceblitzyt . If you want to work with tags, you should find the object earlier, and not just do Tag.transform. But using a transform attached like the Noah example also works. This depends upon yours implementation.

avatar image Noah-1 · Oct 20, 2012 at 06:22 PM 0
Share

Great, I up voted your answer :)

avatar image Iceblitzyt · Oct 20, 2012 at 08:58 PM 0
Share

Brilliant thank you very much, you've expanded my knowledge and solved my issue. Also thanks to everyone else, your inputs have also assisted me in an invaluable way :D

avatar image

Answer by paulaceccon · Oct 20, 2012 at 12:27 AM

I would do this using Vector3.Distance. (http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Distance.html)

So you could do something like this:

 floar dist = Vector3.Distance(object1.transform.position, object2.transform.position);
     if(dist < 20) //or what fits better
          //show the message
  

I hope it helps you. (:

Comment
Noah-1

People who like this

1 Show 2 · 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 Iceblitzyt · Oct 20, 2012 at 05:42 PM 0
Share

So I did what you suggested. Named the 1st person controller player, tagged it player did the same for the cube and tagged it cube. I did this script:

using UnityEngine; using System.Collections;

public class mesagebox : MonoBehaviour { public Transform other; void OnGUI(){ float dist = Vector3.Distance(Player.transform.position, Cube.transform.position); if(dist < 20){ GUI.Box(new Rect(50,50,500,500),"This is a message system! \n message boxes are cool!"); } } }

And it came up with errors, did i so something wrong?

avatar image paulaceccon · Oct 20, 2012 at 05:54 PM 0
Share

I don't know what errors do you received, but about tags... I never use them, I always use Layers, but I guess you couldn't just acess Tag.transform. First, you have to find the object with this tag. (:

http://docs.unity3d.com/Documentation/Components/Tags.html

avatar image

Answer by speedything · Oct 19, 2012 at 03:14 PM

The Unity Scripting reference is in both Javascript and C#. To the top-right of each code box you should have a drop down menu that let's you switch between the two.

Comment

People who like this

0 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 Sonaten · Oct 19, 2012 at 03:20 PM 0
Share

I fell for the very same thing. Using something like an hour before I found out.

avatar image Iceblitzyt · Oct 19, 2012 at 03:25 PM 0
Share

ah I see, thanks for telling me. Now i need to attempt to see how i can make this work :)

avatar image Sonaten · Oct 19, 2012 at 03:39 PM 0
Share

If this is not the solution you have been looking for I've thrown an answer a collider approach to your problem. It should be up soon, provided that the moderator approves :)

avatar image

Answer by Sonaten · Oct 19, 2012 at 03:50 PM

I'd say, take a different approach. Using OnTrigger funtions you could let the physics-engine handle it. Similar to this, script on cube approach:

You will need 2 objects. PlayerObject, give this RigidBody and Collider. Cube, give this Collider and set IsTrigger to true (you will find this in the "inspector view"). Set one of these colliders radius or range to the distance you need. (I'd say, preferably the collider on the Cube)

use this script on the Cube

 using UnityEngine;
 using System.Collections;
 
 public class CubeHandler : MonoBehaviour
 {
     void OnTriggerEnter(Collider target)
     {
         if( target.name == PlayerObject ) {/*GUICODE*/}
     }
 }

start by testing using Debug and checking the console.

 Debug.Log("StringMessage");

I believe it to be possible to swap the handling script, rigidbody, and IsTrigger for a "reversed" way to handle it. (I would suggest that the player should keep a rigidbody if you try this)

Hope this can help you.

Comment

People who like this

0 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 Iceblitzyt · Oct 19, 2012 at 09:40 PM 0
Share

I've given my first person controller the name and tag Player and changed the code (target.name == Player) to accommodate the change added a rigidbody to Player and set the trigger to the cube. however the script doesn't work. Did i do something wrong?

avatar image Iceblitzyt · Oct 19, 2012 at 10:16 PM 0
Share

This is the error : ArgumentException: You can only call GUI functions from inside OnGUI. UnityEngine.GUIUtility.CheckOnGUI () (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/GUIUtility.cs:427) UnityEngine.GUI.Box (Rect position, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/GUI.cs:214) UnityEngine.GUI.Box (Rect position, System.String text) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/GUI.cs:208) mesagebox.OnTriggerEnter (UnityEngine.Collider target) (at Assets/Myscripts/mesagebox.cs:10)

avatar image phodges · Oct 20, 2012 at 05:55 PM 0
Share

Don't take the comment in the hint too literally and attempt to use GUI methods in OnTriggerEnter, instead flag that your message box should be visible and then deal with it in OnGUI.

Also don't forget to handle OnTriggerExit if your intention is to dismiss the message box when the player moves away.

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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Issues building an in-game MsgBox 3 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