How can I make a game object a variable and then use a button to select it.
Thanks.
How can I make a game object a variable and then use a button to select it.
Thanks.
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*;*
_/* 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];*
*}*
*```*
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.