- Home /
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.
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.
Answer by jahroy · Oct 17, 2011 at 12:03 AM
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];
}
If you like it, upvote it! And then mark it as the accepted answer.
$$anonymous$$arked it as correct, but could'nt upvote it, some sort of permission thing.
Answer by Bugcheese · Oct 16, 2011 at 11:56 PM
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.
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.
Your answer
Follow this Question
Related Questions
global variable need advice 1 Answer
How to assign an object to variable C#? 1 Answer
Can't access gameObject from the script it is attached to 2 Answers
Object.find destroy? 2 Answers
Problem with destroying a gameObject when used with targeting 1 Answer