x


How to make text appear when moving next to an object?

i am trying to make a script that makes text appear when you move next to an object. i have used the GUI text but i want it to appear when you move close to an object and dissapear when you move away from the object. is it possible to do this?

please help thanks.

more ▼

asked Mar 30 '10 at 02:42 PM

rahib gravatar image

rahib
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Yes, that's possible. Here's what you do (assuming you are using JS). Create a sphere gameobject, and set it's collider to trigger. Remove the mesh renderer, and position it so that its trigger collider surrounds the gameObject. Next, put this script on it:

function OnTriggerEnter (other : Collider) {
    if (other.CompareTag ("Player")) {         
        SendMessageUpwards("SetTheGUI");
    }
}

function OnTriggerExit (other : Collider) {
    if (other.CompareTag ("Player")) {         
        SendMessageUpwards("UnSetGUI");
    }
}

Next, create another script, and put it on your main character:

var close = false;
var textFieldString = "text field";

function SetTheGUI () {
    close = true;
}

function UnSetGUI () {
    close = false;
}

function OnGUI () {
    if (close) {
        GUI.Label (Rect (25, 25, 100, 30), textFieldString);
    }
}

Now, if your player enters the trigger you made, it will set a boolean variable inside your player script to 'true'. If the value is true, then a GUI text field will appear. Hope this helps!


EDIT: If it's still not working, go over this list, making sure they are all right.

  1. Make sure your collider is surrounding the object - and make sure it's big enough.
  2. Is it set to be a trigger?
  3. Re-copy the code into two seperate files. Save them, and put the first on your trigger, and put the second on your player.
  4. Is your first player controller's tag set to "Player"?

ANOTHER EDIT: Set your trigger's code to this:

function OnTriggerEnter (other : Collider) {        
        SendMessageUpwards("SetTheGUI");
}

function OnTriggerExit (other : Collider) {        
        SendMessageUpwards("UnSetGUI");
}

Hmm. I really can't see anything else that's wrong in my instructions... It all should work. Try one more time, except replace the old trigger code with this above code. Also, recopy the player's code, I made a couple of changes.

more ▼

answered Mar 30 '10 at 03:07 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

do you add the second script to the first person controller or to an object? thanks.

Mar 30 '10 at 04:13 PM rahib

You add it to the first person controller(main character, I called it)

Mar 30 '10 at 04:17 PM e.bonneville

does'nt seem to work :(. do i need to create GUI text?

Mar 30 '10 at 04:32 PM rahib

Hmm. Make sure your first person controller's tag is player. Maybe I've got a typo. I'll go over it again.

Mar 30 '10 at 04:39 PM e.bonneville

yeah ive done evrything you've said except now when i walk into trigger the game pauses.

Mar 30 '10 at 04:59 PM rahib
(comments are locked)
10|3000 characters needed characters left

Elbon96 did a fantastic job here.

Addition is that the Sphere Object needs to be a child of your main character's object, so that the "SendMessageUpwards()" can be received.

more ▼

answered Apr 28 '10 at 10:55 AM

Christophe F gravatar image

Christophe F
229 9 9 20

(comments are locked)
10|3000 characters needed characters left

you need to creat a sphere collider (compomponent> physics> sphere collider. whn the object Selected) set it to "is trigger". Then Attach this code to the player:

var displayMessage = false;

function OnTriggerEnter (other : Collider) {

displayMessage = true; }

function OnGUI () {
if (displayMessage){
  GUI.Label (Rect (10, 10, 100, 20), "Hello World!");
  }
  }

*javascript

this code Does that any trigger you go to, It writes "hllow world" if you want its for Specific object I think you need to Write:

function OnTriggerEnter (other : Collider "object name") {

more ▼

answered Sep 28 '12 at 01:55 PM

orx gravatar image

orx
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5270
x3811
x3418
x2169
x568

asked: Mar 30 '10 at 02:42 PM

Seen: 4448 times

Last Updated: Sep 28 '12 at 01:55 PM