- Home /
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.
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!
Yeah, it's exactly what I'm trying to explain! Sorry, I'm not good to explain things.
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.
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
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. (:
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 : $$anonymous$$onoBehaviour { 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?
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. (:
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.
I fell for the very same thing. Using something like an hour before I found out.
ah I see, thanks for telling me. Now i need to attempt to see how i can make this work :)
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 :)
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.
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?
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/$$anonymous$$yscripts/mesagebox.cs:10)
Don't take the comment in the hint too literally and attempt to use GUI methods in OnTriggerEnter, ins$$anonymous$$d 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.