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 eyeproductions · Apr 18, 2012 at 07:42 AM · guiobjectvisibilitychanging

Controlling visibility of several objects with GUI

Hi.

This is my first attempt at coding with javascript in Unity and I've run into difficulty with what I think should be a relatively simple task.

I have three objects in the scene and I want to control their visibility using three GUI buttons, one for each object. I need it to work so that each button makes an object visible while making the other two invisible.

I have so far got the following script attached to my scene camera:

 function OnGUI () {
 
 
 if (GUI.Button (Rect (20,40,80,20), "Blue")) {
 name = "Sphere-Blue";renderer.enabled = true;
 name = "Sphere-Red";renderer.enabled = false;
 name = "Sphere-Green";renderer.enabled = false;
 
 
     }
 }

Using this I am not getting the behaviour I had hoped for. Ideally, the button named "Blue" would make the "Sphere-Blue" object visible while blocking out the other objects.

Am I even close? Searching the forums has got me to this point but I'm feeling pretty lost now.

Any help would be greatly appreciated.

Kind Regards

Comment
Add comment · Show 3
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 GuyTidhar · Apr 18, 2012 at 07:50 AM 0
Share

you OnGUI() currently does only this:

 name = "Sphere-Green";renderer.enabled = false;

You have one "name" variable, and you accessing the same renderer every time, so only the last ones actually count.

You need to reference each of your objects.

Is "Sphere-Blue" a name of an actual GameObject in your scene on which a renderer is attached?

avatar image eyeproductions · Apr 18, 2012 at 08:01 AM 0
Share

Hi.

Yes, "Sphere-Blue" "Sphere-Green" and "Sphere-Red" are the names of the objects I have in the scene, though I'm not sure about the renderer? I haven't attached any renderers, I'm not even sure what that means.

avatar image GuyTidhar · Apr 18, 2012 at 08:14 AM 0
Share

Check out the script and the notes I listed at the bottom.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by eyeproductions · Apr 18, 2012 at 09:44 AM

Hi.

Yes, "Sphere-Blue" "Sphere-Green" and "Sphere-Red" are the names of the objects I have in the scene, though I'm not sure about the renderer? I haven't attached any renderers, I'm not even sure what that means.

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
avatar image
0

Answer by GuyTidhar · Apr 18, 2012 at 08:08 AM

Add this script:

 #pragma strict

 // Define a button setting data structure
 public class ButtonSwitch
 {
     public var name         : String; // Name of GUI button
     public var renderer        : Renderer; // Reference to actual 3D object
     public var areaOnScreen    : Rect; // Rectangle to place GUI button
 }

 public var buttonAction : ButtonSwitch[]; // An array of all button settings
 
 function OnGUI () 
 {
     var buttonSwitchedIndex : int = -1;

     // Process all button settings
     for(var b=0; b<buttonAction.Length; b++)
     {
             // Draw button with current settings. Check if a button with current settings was click. 
         if ( GUI.Button(buttonAction[b].areaOnScreen, buttonAction[b].name) )
         {
                     // Show 3D object
             buttonAction[b].renderer.enabled = true;
                     // Remember the place in array of the pressed button
             buttonSwitchedIndex = b;
         }
     }
     
     // Shutdown the rest of the buttons
     if ( buttonSwitchedIndex > -1 )
     {
         for(b=0; b<buttonAction.Length; b++)
         {
                     // Unless this is the 3D button we showed, hide button
             if ( buttonSwitchedIndex != b )
                 buttonAction[b].renderer.enabled  = false;
         }
     }
 }

  1. Attach it to a new game object.

  2. You have a list of "Button Action" - set it to "3" as you have 3 button.

  3. Set the name of each of the buttons as you wish to appear on the button label.

  4. Into the "renderer" field - drag the obrect corresponding to that button, e.g. to the "Blue" drag the object "Sphere-Blue".

  5. "Area On Screen" is the rect you set to place the GUI button.

Now, clicking on the gui button should do the trick.

Renderer is the component that draws a 3D object in your game.

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 eyeproductions · Apr 18, 2012 at 09:02 AM 0
Share

Hey, thanks for the help. I'm stubling my way through your script and will post when I think I've got past most of the more stupid questions I have.

avatar image GuyTidhar · Apr 18, 2012 at 09:28 AM 0
Share

No problem mate.

I have added comments to the script.

avatar image eyeproductions · Apr 18, 2012 at 09:40 AM 0
Share

I struggled to follow the steps and got dozens of errors. I'm not even sure where to start with describing them all.

Is there a more simple way to do this?

avatar image GuyTidhar · Apr 18, 2012 at 09:47 AM 0
Share

Have you tried placing this script in your scene following my instructions without touching the script itself?

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

GUI On button click change visibility of array objects 0 Answers

touch 3d object open gui 1 Answer

Level select design approaches. 0 Answers

World object in front of GUI object 3 Answers

Rotating GUI Texture By Angle 2 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