Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 30, 2013 at 04:08 PM by clunk47 for the following reason:

User resolved issue.

avatar image
0
Question by Jammer3000 · Oct 30, 2013 at 12:00 AM · guirigidbodyactivate

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);
         }
 }
Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tomer-Barkan · Oct 30, 2013 at 11:23 AM 0
Share

Please edit your question and format all the code parts as code (5th button from the left above the edit field).

avatar image Jammer3000 · Oct 30, 2013 at 01:11 PM 0
Share

Okay sorry forgot to do that.

avatar image Tomer-Barkan · Oct 30, 2013 at 01:27 PM 0
Share

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?

avatar image Jammer3000 · Oct 30, 2013 at 01:44 PM 0
Share

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;
     }
 }
avatar image Tomer-Barkan · Oct 30, 2013 at 01:49 PM 1
Share

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();
Show more comments

1 Reply

  • Sort: 
avatar image
2

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();
         }
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jammer3000 · Oct 30, 2013 at 01:12 PM 0
Share

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;
             }
         }
     }
avatar image Tomer-Barkan · Oct 30, 2013 at 01:33 PM 0
Share

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.

avatar image Jammer3000 · Oct 30, 2013 at 01:49 PM 0
Share

No it is I just must have deleted it on accident just checked its there

avatar image clunk47 · Oct 30, 2013 at 04:02 PM 0
Share

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.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How To Make GUI Buttons Load/Quit 1 Answer

How to save and load text in a GUI TextArea 1 Answer

GUI Sliders in the same script. Question (Js.) 1 Answer

Need help with my script 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges