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 Bones20 · Oct 16, 2011 at 03:30 PM · objectgamevariableselecting

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.

Comment
Add comment · Show 1
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 syclamoth · Oct 16, 2011 at 03:38 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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];

}

Comment
Add comment · Show 3 · 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 Bones20 · Oct 17, 2011 at 02:28 AM 0
Share

Thanks, I know I have been hard.

avatar image syclamoth · Oct 17, 2011 at 02:29 AM 0
Share

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

avatar image Bones20 · Oct 17, 2011 at 03:19 PM 0
Share

$$anonymous$$arked it as correct, but could'nt upvote it, some sort of permission thing.

avatar image
0

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.

Comment
Add comment · Show 1 · 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 jahroy · Oct 17, 2011 at 12:44 AM 0
Share

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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