- Home /
A big problem with my script..
Hello!
var showTexture : boolean = false;
var someTexture : Texture;
function OnTriggerEnter (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
showTexture = true;
ShowPage();
}
}
function ShowPage() {
if(showTexture == true && Input.GetKeyDown(KeyCode.E)){
OnGUI();
}
else
{
showTexture = false;
}
}
function OnGUI() {
{
GUI.DrawTexture(Rect(10,10,60,60), someTexture, ScaleMode.ScaleToFit, false, 10.0f);
}
}
function OnTriggerExit() {
Destroy(gameObject);
}
I made this Javascript and the problem is that my Texture is showing up all the time. How can I fix it so that the texture will show up only when I enter the trigger and press "E"? Help pls
i never tried javascript but i think i can help ,what's inside "function ShowPage" should be inside the update function ins$$anonymous$$d
Could you not just disable the component until OnTriggerEnter, then OnTriggerExit disable it?
Answer by Hellium · Sep 16, 2017 at 05:10 PM
var inTrigger : boolean = false;
var showTexture : boolean = false;
var someTexture : Texture;
function OnTriggerEnter (theCollider : Collider)
{
if (theCollider.tag == "Player")
{
inTrigger = true;
}
}
function Update()
{
if( inTrigger && Input.GetKeyDown(KeyCode.E) )
showTexture = true ;
}
function OnGUI()
{
if( showTexture )
GUI.DrawTexture(Rect(10,10,60,60), someTexture, ScaleMode.ScaleToFit, false, 10.0f);
}
function OnTriggerExit()
{
inTrigger = false ;
Destroy(gameObject);
}
Answer by ransomink · Sep 18, 2017 at 04:14 AM
you could try this:
#pragma strict
var keyPressed : boolean = false;
var showTexture : boolean = false;
var someTexture : Texture;
function Update()
{
if ( Input.GetKeyDown( KeyCode.E ) )
keyPressed = true;
}
function OnGUI()
{
if ( showTexture && keyPressed )
GUI.DrawTexture( Rect( 10, 10, 60, 60 ), someTexture, ScaleMode.ScaleToFit, false, 10.0f );
}
function OnTriggerEnter( theCollider : Collider )
{
if ( theCollider.gameObject.CompareTag( "Player" )
{
showTexture = true;
}
}
function OnTriggerExit()
{
showTexture = false;
Destroy( gameObject );
}
I would definitely look into learning /switching to C# because they're making Unityscript obsolete soon. It's only a matter of syntax for the Unity API.
Your answer
Follow this Question
Related Questions
Show GUI Texture on Trigger and Instantiate Object if Button pressed while In Trigger? 1 Answer
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
GUItexture displayed after al triggers activated 1 Answer
OnGUI button created by a foreach loop 4 Answers
My GUITexture looks awful 2 Answers