I need your help guys

Hi guys
I lost my old account, so I had to create a new
How can I assign the same key? Whatever text appeared and disappeared one key
(Press once, the text appeared, pressed a second time, the text disappeared)

Here’s the code

var nearObject : boolean = false;
var appearText : boolean = false;
var message : String;
public var guiSkin : GUISkin;


function OnTriggerEnter(other : Collider)
{
    if(other.gameObject.tag == "Player")
    {
            nearObject = true;
    }
}
function OnTriggerExit(other : Collider)
{
    if(other.gameObject.tag == "Player")
    { 
            nearObject = false;
    }
}

function OnGUI(){
if(Input.GetKeyUp (KeyCode.E) && nearObject) {
appearText = true;
}
if(appearText){
//show text
Time.timeScale = 0;
GUI.skin = guiSkin;
GUI.Label(Rect(Screen.width/2 - 250, Screen.height- 100,700,150), message);
}
//hide text
if(Input.GetKeyDown (KeyCode.E) && nearObject){
Time.timeScale = 1;
appearText = false;
}

}

you need to include the “appearText” boolean in the if statements on lines 23 and 33

And it is possible more in detail?
I’m just new to scripting

as it is tried with this code
but that that did not leave me:hushed:

private var isPressed = false;
function Update {
if (Input.GetKeyDown(KeyCode.E)) {
if (!isPressed) {
//put show text here
isPressed = true;
}
else {
//hide text
isPressed = false;
}
}
}

:(:frowning:

// show text
if(Input.GetKeyDown(KeyCode.E) && nearObject && !appearText)
{
appearText = true;
}

although, on revisiting it, you could probably do away with having two if statements and just toggle.

{
    if(Input.GetKeyUp (KeyCode.E) && nearObject) 
    {
// flip appearText value
        appearText = !appearText;
    }
    if(appearText)
    {
        //show text
        Time.timeScale = 0;
        GUI.skin = guiSkin;
        GUI.Label(Rect(Screen.width/2 - 250, Screen.height- 100,700,150), message);
    }
}

screen just freezes + is not visible text

I am not sure if a GUI element can appear with Time.timeScale being 0. I mean, if it animates in, it needs a timescale, right? Things like NGUI or HOTween have their own timescale, so maybe work with those?

What behavior are you trying to achieve anyway? I bet three if conditions are one too many.

I’ve got one button to respond to the appearance of the text, and the text fading
In this case “E”
Example:
I went to the subject, I press the button “E” text appears, once again shake down the “E” disappears tex

Does it work if you leave the timeScale at 1f?

It does not work; c

ah, put the toggle code into the update function

#pragma strict

var nearObject : boolean = false;
var appearText : boolean = false;
var message : String;
public var guiSkin : GUISkin;


function OnTriggerEnter(other : Collider)
    {
        if(other.gameObject.tag == "Player")
        {
            nearObject = true;
        }
    }
    function OnTriggerExit(other : Collider)
        {
            if(other.gameObject.tag == "Player")
            {
                nearObject = false;
            }
        }

        function Update()
        {
            if(Input.GetKeyUp (KeyCode.E) && nearObject)
            {
                // flip appearText value
                appearText = !appearText;
            }
        }

        function OnGUI()
        {

            if(appearText)
            {
                //show text
                Time.timeScale = 0;
                GUI.skin = guiSkin;
                GUI.Label(Rect(Screen.width/2 - 250, Screen.height- 100,700,150), message);
            }
        }
1 Like

on another note, are you particularly attached to using the OnGUI approach? It was replaced by the new UI system when version 4.6/5 was released. Might be worth learning the new way given that OnGUI is now really only useful for custom editor scripting…

2 Likes

thanks a lot dude
it works:p:smile:

 function Update()
        {
            if(Input.GetKeyUp (KeyCode.E) && nearObject &&! audio.isPlaying)
            {
                // flip appearText value
                appearText = !appearText;
                Time.timeScale = 1;
                audio.Play ();

how to make that sound is played once when the key is pressed?
Every time I press the button “E” sound is played

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/sound-effects-scripting?playlist=17096