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 /
  • Help Room /
avatar image
0
Question by Mahunreah · Mar 30, 2018 at 08:21 PM · prefabbutton

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"

Comment
Add comment
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

1 Reply

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

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));
         }

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

159 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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