- Home /
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));
}
}
}
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.
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());
}
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;
}
}