x


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

more ▼

asked Apr 18 '12 at 07:42 AM

eyeproductions gravatar image

eyeproductions
0 1 1 1

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?

Apr 18 '12 at 07:50 AM GuyTidhar

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.

Apr 18 '12 at 08:01 AM eyeproductions

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

Apr 18 '12 at 08:14 AM GuyTidhar
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Apr 18 '12 at 09:44 AM

eyeproductions gravatar image

eyeproductions
0 1 1 1

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 18 '12 at 08:08 AM

GuyTidhar gravatar image

GuyTidhar
2.2k 4 8 13

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.

Apr 18 '12 at 09:02 AM eyeproductions

No problem mate.

I have added comments to the script.

Apr 18 '12 at 09:28 AM GuyTidhar

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?

Apr 18 '12 at 09:40 AM eyeproductions

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

Apr 18 '12 at 09:47 AM GuyTidhar
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3694
x1095
x64
x25

asked: Apr 18 '12 at 07:42 AM

Seen: 1026 times

Last Updated: Jan 25 at 06:04 PM