- Home /
Is it possible to start a game and get GUI-button act as like its already pressed? (Javascript)
Hello, I have a problem because I've created two GUI-buttons that hides/disables and unhides/enables gameObjects renderer. And I need to second one act like its already pressed once when the game starts or something like that.
The script I have:
var S1 : GameObject;
var S2 : GameObject;
var S3 : GameObject;
var S4 : GameObject;
var K1 : GameObject;
var K2 : GameObject;
var K3 : GameObject;
var K4 : GameObject;
function OnGUI () {
if(GUI.Button(Rect(220, 140, 80, 20),"HOID2"))
{
S1.GetComponent.<Renderer>().enabled = false;
S2.GetComponent.<Renderer>().enabled = false;
S3.GetComponent.<Renderer>().enabled = false;
S4.GetComponent.<Renderer>().enabled = false;
K1.GetComponent.<Renderer>().enabled = true;
K2.GetComponent.<Renderer>().enabled = true;
K3.GetComponent.<Renderer>().enabled = true;
K4.GetComponent.<Renderer>().enabled = true;
}
if(GUI.Button(Rect(220, 100, 80, 20),"HOID"))
{
S1.GetComponent.<Renderer>().enabled = true;
S2.GetComponent.<Renderer>().enabled = true;
S3.GetComponent.<Renderer>().enabled = true;
S4.GetComponent.<Renderer>().enabled = true;
K1.GetComponent.<Renderer>().enabled = false;
K2.GetComponent.<Renderer>().enabled = false;
K3.GetComponent.<Renderer>().enabled = false;
K4.GetComponent.<Renderer>().enabled = false;
}
}
So is it possible to create something like that the "HOID2"-button already does the thing even it's never been pressed or is this a lost cause? Hope you understand what I mean, thanks in advance.
Answer by $$anonymous$$ · Jul 02, 2015 at 06:31 PM
Try this:
#pragma strict
var S1 : GameObject;
var S2 : GameObject;
var S3 : GameObject;
var S4 : GameObject;
var K1 : GameObject;
var K2 : GameObject;
var K3 : GameObject;
var K4 : GameObject;
function Start () {
turnOneOnAndTheOtherOff(false);
}
function OnGUI () {
if(GUI.Button(Rect(220, 140, 80, 20),"HOID2"))
{
turnOneOnAndTheOtherOff(false);
}
if(GUI.Button(Rect(220, 100, 80, 20),"HOID"))
{
turnOneOnAndTheOtherOff(true);
}
}
function turnOneOnAndTheOtherOff(value : boolean)
{
S1.GetComponent.<Renderer>().enabled = value;
S2.GetComponent.<Renderer>().enabled = value;
S3.GetComponent.<Renderer>().enabled = value;
S4.GetComponent.<Renderer>().enabled = value;
K1.GetComponent.<Renderer>().enabled = !value;
K2.GetComponent.<Renderer>().enabled = !value;
K3.GetComponent.<Renderer>().enabled = !value;
K4.GetComponent.<Renderer>().enabled = !value;
}
I've just tested it and works just fine
The key to answer your question is to use the Start function provided by $$anonymous$$onoBehaviour: $$anonymous$$onoBehaviour.Start()
Hi,
can u try this in the way,made some change on the code.Assign the gameobject to the related GameObject in the script.
var S1 : GameObject;
var S2 : GameObject;
var S3 : GameObject;
var S4 : GameObject;
var $$anonymous$$1 : GameObject;
var $$anonymous$$2 : GameObject;
var $$anonymous$$3 : GameObject;
var $$anonymous$$4 : GameObject;
function Start () {
turnOneOnAndTheOtherOff(false);
}
function OnGUI () {
if(GUI.Button(Rect(220, 140, 80, 20),"HOID2"))
{
turnOneOnAndTheOtherOff(false);
}
if(GUI.Button(Rect(220, 100, 80, 20),"HOID"))
{
turnOneOnAndTheOtherOff(true);
}
}
function turnOneOnAndTheOtherOff(value : boolean)
{
S1.SetActive(value);
S2.SetActive(value);
S3.SetActive(value);
S4.SetActive(value);
$$anonymous$$1.SetActive(!value);
$$anonymous$$2.SetActive(!value);
$$anonymous$$3.SetActive(!value);
$$anonymous$$4.SetActive(!value);
}
Your answer
Follow this Question
Related Questions
Created game objects rollover show guy text 0 Answers
Trouble getting GUI phone to open/close 1 Answer
Enter Trigger, play animation(s) and sound FX 1 Answer
Setting Scroll View Width GUILayout 1 Answer
GUI GameObject Button? 1 Answer