Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JamesBerry · Sep 27, 2016 at 10:39 AM · scripting problembuttoneventsystemeventsselection

How to reference the previously selected game object?

I' am Writing a script for a button click to instantiate a game object into my play field. My play field is a grid of buttons so when you select a button from the play field it will give you another selection of buttons with different objects to instantiate, I 'am trying to reference the button that was clicked in the play field in the other buttons script when it is clicked, so using "EventSystem.current.currentSelectedGameObject" wouldnt work right? so could i use "EventSystem.current.lastSelectedGameObject"? the other thing is that when i try just referencing the selected game object and using Debug.Log. to print its name its not returning with anything , see Below

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class TurretScript : MonoBehaviour
 {
     public Sprite Turret;
 
     private GameObject selButton;
 
     void Awake()
     {
         selButton = GameObject.Find(EventSystem.current.currentSelectedGameObject.name);
     }
 
     void start()
     {
         if (GameObject.Find(EventSystem.current.currentSelectedGameObject.name) != null)
         {
             Debug.Log((selButton.name));
         }
     }
 }
Comment
Add comment · Show 3
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 Zodiarc · Sep 28, 2016 at 12:11 PM 0
Share

The only thing i can think of is to generate the second set of buttons at runtime when you click the first and pass them the current object or have some kind of global data holder which you can populate on click of the first button and read it in the second.

avatar image JamesBerry Zodiarc · Sep 28, 2016 at 12:21 PM 0
Share

So something like a dictionary?

avatar image Zodiarc JamesBerry · Sep 28, 2016 at 12:24 PM 0
Share

Yes kind of. In my current project I use the first approach. I can post a code example later on.

Here's an example of how it works https://j.gifs.com/rk3xPk.gif

Here´s the example code:

 public void build$$anonymous$$ountSelectionButtons(string path, string name, RectTransform scrollView)
         {
             Button tempButton;
             if (this.mechaContainer.cockpit != null)
             {
                 $$anonymous$$echaParts.Cockpit cockpit = this.mechaContainer.cockpit.GetComponent<$$anonymous$$echaParts.Cockpit>();
                 if(cockpit.left$$anonymous$$ount != null)
                 {
                     tempButton = this.createTempButton(scrollView);
                     tempButton.GetComponentInChildren<Text>().text = "Left Chest";
                     tempButton.onClick.AddListener(() => this.createWeapon(path, name, cockpit.left$$anonymous$$ount, ref this.mechaContainer.leftInnerWeapon, Side.LEFT));
                     tempButton = null;
                 }
                 if (cockpit.right$$anonymous$$ount != null)
                 {
                     tempButton = this.createTempButton(scrollView);
                     tempButton.GetComponentInChildren<Text>().text = "Right Chest";
                     tempButton.onClick.AddListener(() => this.createWeapon(path, name, cockpit.right$$anonymous$$ount, ref this.mechaContainer.rightInnerWeapon, Side.RIGHT));
                     tempButton = null;
                 }
                 if (cockpit.top$$anonymous$$ount != null)
                 {
                     tempButton = this.createTempButton(scrollView);
                     tempButton.GetComponentInChildren<Text>().text = "Top";
                     tempButton.onClick.AddListener(() => this.createWeapon(path, name, cockpit.top$$anonymous$$ount, ref this.mechaContainer.topWeapon, Side.TOP));
                     tempButton = null;
                 }
             }
             if(this.mechaContainer.armor != null)
             {
 
             }
             tempButton = this.createTempButton(scrollView);
             tempButton.GetComponentInChildren<Text>().text = "Back";
             tempButton.onClick.AddListener(() => this.backToWeapons());
         }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by astracat111 · May 31, 2018 at 07:58 PM

 Just figured this out:

 public EventSystem eventSystem; //select event system in editor
 public GameObject lastSelectedGameObject;
 private GameObject currentSelectedGameObject_Recent;

 void Update() {
    GetLastGameObjectSelected();
 }


 private void GetLastGameObjectSelected() {
         if (eventSystem.currentSelectedGameObject != currentSelectedGameObject_Recent) {

             lastSelectedGameObject = currentSelectedGameObject_Recent;

             currentSelectedGameObject_Recent = eventSystem.currentSelectedGameObject;
         }
  }
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

UI: Mouse events not working 1 Answer

Button doesn't work when returning to scene (android) 1 Answer

Where is my OnClick in the button script? 1 Answer

How to get argument values from persistent listener of UnityEvent 2 Answers

Should I initialize my Unity events? 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