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 /
avatar image
0
Question by zirkl003 · Jul 08, 2012 at 07:19 PM · guirendertoggle

How to make gameObject and GUI button invisible/visible?

I'm trying to use GUI buttons as switches between two lights; a flashlight, and the sun. I also want the buttons that operate the flashlight rotation to disappear when the sun is activated. However, I can't get the flashlight and buttons to become invisible. Also the reset button, which is supposed to reset the flashlight's position as well as the sliders, isn't working properly; it resets the sliders without moving the flashlight.

 var fLight: Light;
 var sunLight: Light;
 var flashlight: GameObject;
 private var fon = false;
 private var son = false;
 var startPosition : Vector3;
 var startRotation : Quaternion;
 var vSliderValue: float = 0.0;
 var hSliderValue: float = 0.0;
 private var reset = false; // object resets if true;
 
 function Start()
 {
  fLight.light.intensity = .51;
  sunLight.light.intensity = 0;
  startPosition = transform.position;
     startRotation = transform.rotation;
 }
 function Update()
 {
  if (fon) sunLight.light.intensity = 0;
  if (fon) fLight.light.intensity = .51;
  if (fon) flashlight.gameObject.active = true;
  if (son) fLight.light.intensity = 0;
  if (son) sunLight.light.intensity = .5;
  if (son) flashlight.gameObject.active = false;
  if (son) reset.active = false;
  if (son) hSliderValue.active = false;
  if (son) vSliderValue.active = false;
  if (vSliderValue) flashlight.transform.rotation.eulerAngles.x = vSliderValue;
  if (hSliderValue) flashlight.transform.rotation.eulerAngles.z = hSliderValue;
  if (reset) transform.position = startPosition;
     if (reset) transform.rotation = startRotation;
     if (reset) vSliderValue = 0;
     if (reset) hSliderValue = 0;
 }
 
 function OnGUI()
 {
  vSliderValue = GUI.VerticalSlider(Rect(10,20,10,100), vSliderValue, -50, 50);
     hSliderValue = GUI.HorizontalSlider(Rect(20,10,100,10), hSliderValue, -50, 50);
     reset = GUI.Button(Rect(40,40,50,50), "Reset");
  fon = GUI.Button(Rect(900,10,100,100), "Flashlight");
  son = GUI.Button(Rect(900,120,100,100), "Sun Light");
 }
  
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by whydoidoit · Jul 08, 2012 at 07:23 PM

Ok so there is quite a lot wrong with your style of coding here - you should not be doing anything in Update to start with and you should make all of your ifs multi line using { and }.

You can have all of your if logic in the OnGUI -

 fon = GUI.Button(Rect(900,10,100,100). "Flashlight");
 if(fon) {
      sunLight.light.intensity = 0;
      flight.light.intensity = 0.51;
     //etc etc
 }

reset.active makes no sense. Add a #pragma strict to the top of your code to reveal some of the errors. I can't see where you set reset to true in this code either.

Comment
Add comment · Show 5 · 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 zirkl003 · Jul 08, 2012 at 07:50 PM 0
Share

Sorry it's sloppy! Not entirely sure what I'm doing if that wasn't already obvious. I've moved all of the logic to OnGUI like you said. Something still isn't right with the reset, as it will only move the sliders back. What should I use ins$$anonymous$$d of .active to hide buttons in the GUI and to hide the flashlight?

avatar image whydoidoit · Jul 08, 2012 at 07:54 PM 0
Share

:) Sloppy is O$$anonymous$$ - but it is clearer when you keep that logic together. Don't put code in Update when you don't need it :)

So the reset button should do something like:

  reset = GUI.Button(Rect(40,40,50,50), "Reset"));
  if(reset) {
     flashlight.gameObject.active = true;
     //etc

  }

You could use light.enabled rather than disabling the whole gameObject.

avatar image zirkl003 · Jul 08, 2012 at 08:08 PM 0
Share

When I use flashlight.gameObject.active = false; it does indeed become 'inactive' but I also want it hidden. How can I do that? I haven't been able to get a .renderer to work at all.

This is what I have for reset, but the flashlight won't go back to startPosition/Rotation.

reset = GUI.Button(Rect(40,40,50,50), "Reset"); if (reset) { reset = true; transform.position = startPosition; transform.rotation = startRotation; vSliderValue = 0; hSliderValue = 0; }

avatar image whydoidoit · Jul 08, 2012 at 08:19 PM 0
Share

Well you can do:

   for(var r : Renderer in GetComponentsInChildren(Renderer))
   { 
       r.enabled = false;
   }
avatar image Dhaiku · Aug 15, 2012 at 01:04 PM 0
Share

code in the update is better then the ongui

on gui runs twice every frame

update only once

so if you put the same code in the ongui, it's twice as heavy for your computer

avatar image
0

Answer by zirkl003 · Jul 17, 2012 at 01:00 AM

I don't know why I was completely unable to hide the flashlight in this scenario; instead, I moved it out of the camera's view and back again (using transform) each time the 'reset' button is hit.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

How can I set up a series of OnGUI Toggle buttons in a for loop? 1 Answer

Toggle Enabled/Disabled script? 2 Answers

GUI Toggle Using Button Style Remain Active 1 Answer

Change something in a script with other script 1 Answer


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