- Home /
User resolved issue.
How to activate GUI button only if somethings true?
Hi want i want my code to do is only access and activate the gravity on the rigidbody if blue 1 && blue 2 == true, but i can't find anywhere how to create a gui button inside of an if statemen thats not in an OnGUI() function? Here's my code if you could please help me I would greatly appreciate it!!(:
var blue2 = false;
var red2 = false;
var destroyedObject : GameObject;
var myLevelResetLost : String;
var myLevelResetWin : String;
var rigidbodyTrigger : Rigidbody;
function OnTriggerEnter (mycollider : Collider) {
if(mycollider.tag == "Blue1"){
blue1 = true;
Debug.Log("blue1");
}
if(mycollider.tag == "Red2"){
blue2 = true;
Debug.Log("red2");
}
if(blue1 && red2 == true){
Debug.Log("It worked");
}
}
function OnGUI()
{
if (GUI.Button(Rect(10,70,100,40),"Drop Ball"))
{
attachedRigidbody = rigidbodyTrigger.rigidbody;
attachedRigidbody.useGravity = true;
}
}
function OnCollisionEnter(mycollision : Collision)
{
if (mycollision.gameObject.tag == "Door")
{
Destroy(destroyedObject, 1);
Application.LoadLevel(myLevelResetLost);
}
if(mycollision.gameObject.tag == "EndGame")
{
Application.LoadLevel(myLevelResetWin);
}
}
Please edit your question and format all the code parts as code (5th button from the left above the edit field).
Let me get this straight - you want the button to appear ALWAYS, but only set the gravity if it's clicked and blue1 && blue2 are true?
And what's wrong with your code? How is it not functioning correctly?
Your absolutely right, but I figured it out. But now I am wondering if there's anyway to run an animation on another gameobject from this script and it has to be in the if statement that checks if blue1 && blue2 are true. Is it possible cause I have a script(the one below) that runs the animation when I click "m" but i only want that to run if blue1 && blue2 are true.
var body = gameObject;
function Update()
{
if(Input.Get$$anonymous$$eyDown("m"))
{
animation.Play();
body.collider.enabled = false;
}
}
If you solved your problem, please share the solution and accept the answer that helped you solve.
You can use GameObject.Find()
to find another game object and then run the animation:
GameObject.Find("OtherObject").animation.Play();
Answer by clunk47 · Oct 30, 2013 at 12:09 AM
void OnGUI()
{
if(somethingIsTrue)
{
if(GUI.Button(new Rect(0, 0, 64, 64), "ClickMe")
{
DoSomething();
}
}
}
I already tried that by doing this and it doesn't display the button at all.
function OnGUI()
{
if(blue1 && blue2 == true){
if (GUI.Button(Rect(10,70,100,40),"Drop Ball"))
{
attachedRigidbody = rigidbodyTrigger.rigidbody;
attachedRigidbody.useGravity = true;
}
}
}
I think I get it.
Your problem is that blue1 is not a class variable.
Add
var blue1 = false
to the beginning of your code.
No it is I just must have deleted it on accident just checked its there
Just use if(blue1 && blue2), no need for the == true if(this) means if (this == true), if(!this) means if(this == false). Doesn't really matter on that part, but be sure blue1 and blue2 are BOTH TRUE in order for your button to show up.