x


Using a button to select an object set as a variable.

How can I make a game object a variable and then use a button to select it.

Thanks.

more ▼

asked Oct 16 '11 at 03:30 PM

Bones20 gravatar image

Bones20
21 3 3 5

Could you be more specific? What do you mean, 'make a game object a variable'? Do you mean, 'how do I reference a game object in a script'?

Also, please explain what you mean by 'use a button to select it'- do you mean in the editor, or in your game? 'selecting' is a high-level concept which can be implemented in any number of ways- try thinking through exactly what it is that you want to do, and then revise your question to be more... answerable.

Also, questions usually have at least one question mark in them? It kind of helps you parse the sentences.

Oct 16 '11 at 03:38 PM syclamoth
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Here is a simple script that draws a button for each Transform in an array of Transforms.

If you attach this script to a GameObject, you will be able to populate the array of Transforms (named arrayOfTransforms) by dragging GameObjects onto the slot in the Inspector.

The OnGUI function draws a button for each Transform in the array. If the user clicks the button, the Transform that corresponds to the button will be assigned to the variable chosenTransform so that you can manipulate it using code.

/* a list of things that contain Transforms (GameObjects) */

var arrayOfTransforms  :  Transform [];

/* rectangle used for the GUI */

var guiRectangle : Rect = Rect(200, 200, 300, 300);

/* currently selected transform */

var chosenTransform : Transform;


function OnGUI ()
{
    /* make sure the array isn't null or empty */

    if ( ! arrayOfTransforms || arrayOfTransforms.length == 0 ) {
        Debug.LogError("Please initialize array of transforms...");
        return;
    }

    GUILayout.BeginArea(guiRectangle);

    /* use a for loop to iterate over the array of Transforms */

    for ( var i = 0; i < arrayOfTransforms.length; i ++ ) {

        /* use a variable to keep track of the current Transform */

        var thisTransform = arrayOfTransforms[i];

        /* draw a button for each item in the array */

        if ( GUILayout.Button("Choose " + thisTransform.name) ) {

            /* if the button is pressed, store the current transform */

            chosenTransform = thisTransform;
        }
    }

    /* draw a box that shows the name of the chosen Transform */

    GUILayout.Box("You have chosen: " + chosenTransform.name);

    /* do stuff to the chosen transform with a function */

    manipulateChosenTransform();

    GUILayout.EndArea();
}

/*
 * this function gets executed every frame
 * because it is called in OnGUI() above. 
 * 
 * you can use a function like this to do anything to
 * the selected Transform, which is set in OnGUI and 
 * referenced below
 *
 */

function manipulateChosenTransform ()
{
    /* get outta here if chosenTransform is null */

    if ( ! chosenTransform ) {
        return;
    }

    print("You have chosen to do something to " + chosenTransform.name);
    print("Here is its current position: " + chosenTransform.position);

    /* this is where you might draw a slider to change transparency */
}

/* the Awake function gets called automatically when the game starts */

function Awake ()
{
    if ( ! arrayOfTransforms || arrayOfTransforms.length == 0 ) {
        Debug.LogError("Please initialize array of transforms...");
        return;
    }

    /* set chosenTransform to the first item in the array */

    chosenTransform = arrayOfTransforms[0];
}
more ▼

answered Oct 17 '11 at 12:03 AM

jahroy gravatar image

jahroy
3.2k 14 18 41

Thanks, I know I have been hard.

Oct 17 '11 at 02:28 AM Bones20

If you like it, upvote it! And then mark it as the accepted answer.

Oct 17 '11 at 02:29 AM syclamoth

Marked it as correct, but could'nt upvote it, some sort of permission thing.

Oct 17 '11 at 03:19 PM Bones20
(comments are locked)
10|3000 characters needed characters left

Hello I'm assuming this would help, Say you want to select a GameObject called "Cube" in the hierarchy, then the following code would do that

Selection.activeGameObject = GameObject.Find("Cube");

Of course all you need is to attach this script to a button now. Hope that helps, if not please be more specific.

more ▼

answered Oct 16 '11 at 11:56 PM

Bugcheese gravatar image

Bugcheese
1

This is how you would cause a GameObject to be selected in the hierarchy.

The question is how do you select a GameObject and manipulate it with a script using the GUI.

Oct 17 '11 at 12:44 AM jahroy
(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:

x1089
x819
x794
x19

asked: Oct 16 '11 at 03:30 PM

Seen: 718 times

Last Updated: Oct 17 '11 at 03:19 PM