GameObject has button attached. I want to use the GameObject as the value to hand over when the button is clicked - by script, but how?
Hi =)
I could delete my earlier question, but stumbled upon a (hopefully) much simpler problem.
I made a GameObject prefab called "shopslot". shopslot has a button attached, which takes another GameObject's script's function called "AddItem (Gameobject shop)" and the value I want to hand over is the GameObject shopslot itself, to which the clicked button is attached.
Currently, I have a List of five Items. For each Item in this list, a new instance of shopslot is being generated. (Works like a charm). I was able to make them search for the GameObject with the needed AddItem-Script attached inside the scene. I've testet it with a
void Test () {Debug.Log("Test");}
I can access it and it works.
The thing is: I have no clue about how to hand over the GameObject the player clicked on. It was simple when the shop objects were numbered and were not generated during runtime, but now, it's a different matter. On runtime, the Objects are being generated with names and numbers (for example: "shop 1", "shop 2",...), but I feel mentally stuck on how to tell my function about this.
This is how it looks in my code right now - obviously, the "???" are just there to show where my knowledge/ideas/gutfeeling took a break =( :
for (int i = 0; i < buttons.Count; i++)
{
buttons[i].onClick.AddListener(() => handel.AddItem(???));
}
I have tried it like this:
for (int i = 0; i < buttons.Count; i++)
{
buttons[i].onClick.AddListener(() => handel.AddItem(UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject));
}
It felt like the right path, but the editor puts a red line unter the whole UnityEngine.Event...-stuff.
The "AddItem"-Function looks like this in its main script:
public void AddItem(ShopKeeperSlot shop)
{
[lots of things happening and working]
}
Can somebody here tell me what my error is and how I might fix it, please :)?
Ah, maybe it would help if I would show the whole script where the shopslots are being generated. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class MaterialShop : ItemsInventoryShopLager
{
public ShopKeeperSlot shopslotPrefab;
public Transform shopslotGrid;
public Handel handel;
public List<Button> buttons = new List<Button>();
void Start () {
handel = (Handel)FindObjectOfType(typeof(Handel));
foreach (Item i in items)
{
if (i.warengruppe == "Craftingmaterial")
{
shopSlots.Add(Instantiate(shopslotPrefab, shopslotGrid));
}
}
GetButtons();
AddListener();
for (int i = 0; i < shopSlots.Count; i++)
{
shopSlots[i].name = "ShopSlot " + i;
shopSlots[i].item = items[i];
shopSlots[i].ItemAnzeige(items[i]);
}
}
void GetButtons ()
{
for (int i = 0; i < objects.Length; i++)
{
buttons.Add(objects[i].GetComponent<Button>());
}
}
public void AddListener ()
{
for (int i = 0; i < buttons.Count; i++)
{
buttons[i].onClick.AddListener(() => handel.AddItem(shopSlots[i]));
}
}
}
This time, I put "shopSlots[i]" in the brackets, but got an error as a result when I click on the button in runtime: "ArgumentOutOfRangeException: Argument is out of range. Parameter name: index"
Answer by Mahunreah · Mar 31, 2018 at 09:01 AM
Heureka! To everyone with a similar problem, I found the solution to mine.
If your button is called "b", you can access the GameObject it is attached to by using something like this (of course, your names will vary, but I hope, it will give you the right direction =)! ).
foreach (Button b in buttons)
{
b.onClick.AddListener(() => handel.AddItem(b.GetComponentInParent<ShopKeeperSlot>().thisshopkeeperSlot));
}
Your answer
Follow this Question
Related Questions
Assigning button On Click() with a prefab 0 Answers
Screen Space - Camera : Not displaying button size correctly during play 0 Answers
Solved: Cant get onclick assignet to prefabs button 1 Answer
Buttons are shrinking in size on a mobile screen 0 Answers
How to creating a Gameobject from a prefab via scripted UI BUTTON 1 Answer