• 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
Question by SunilUL · Aug 29, 2018 at 06:29 AM · gameobjectbuttonchange material

How to find the index of the clicked UI button?

Hi,
I want help with picking and adding materials to the Object dynamically

Here is my process:

Will click on a GameObject, The variants of the gameObject will appear in a UI called Customize. The UI will have Variant buttons and clicking on a particular Variant button, the material associated with that variant has to be applied to GameObject. The GameObject will have a script and here is what I'll define the variants: alt text
and here is how the UI is alt text

Can someone please help me with finding the associated variant materials on click and adding them to the game object?
It is also great if i can just know the index of element and variant

here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class CSV_ObjectCustomizer1 : MonoBehaviour {
 
     public List<ProductsYo> Elements = new List<ProductsYo> ();
     private GameObject MainCanvas = null;
     private GameObject CSVItems = null;
 
     void OnMouseDown(){
         if (Input.GetMouseButtonDown (0)) {
             OnPupulate ();
         }
     }
 
     void OnPupulate(){
         MainCanvas = GameObject.Find ("CSV_Content");
         if (MainCanvas.transform.childCount>0) {
             ClearChildren ();
         }
         for (int i = 0; i < Elements.Count; i++) {
             GameObject CSV_ItemPanel = Instantiate(Resources.Load ("CSV_ItemPanel") as GameObject);
             if (CSV_ItemPanel.GetComponentInChildren<Text> ().transform.name == "ItemName") {
                 CSV_ItemPanel.GetComponentInChildren<Text> ().text = Elements [i].ItemName;
             }
             CSV_ItemPanel.transform.SetParent (MainCanvas.transform, false);
 
 
             for (int j = 0; j < Elements [i].Variants.Count; j++) {
                 Debug.Log (Elements[i].ItemName+ Elements [i].Variants [j].VariantName);
                 GameObject CSV_ItemButton = Instantiate (Resources.Load ("CSV_ItemButton") as GameObject);
                 CSV_ItemButton.transform.Find("CSV_VariantName").gameObject.GetComponent<Text>().text = Elements[i].Variants[j].VariantName;
                 CSV_ItemButton.transform.Find ("CSV_VariantIcon").gameObject.GetComponent<Image> ().sprite = Elements [i].Variants [j].VariantIcon;
                 CSV_ItemButton.transform.SetParent (CSV_ItemPanel.transform.Find("CSV_Items"), false);
 
                 //Here is what i'm stuck:
                 CSV_ItemButton.GetComponent<Button> ().onClick.AddListener (() => OnVariantButtonClicked(i,j));
             }
         }
 
 
     }
 
     void ClearChildren(){
         MainCanvas = GameObject.Find ("CSV_Content");
         Transform[] Children = MainCanvas.GetComponentsInChildren<RectTransform> ();
         while (MainCanvas.transform.childCount > 0)
         {
             Transform child = MainCanvas.transform.GetChild(0);
             Destroy(child.gameObject);
         }
     }
 
     //Here is what i'm stuck
     void OnVariantButtonClicked(int ElementIndex, int VariantIndex){
         Debug.Log (ElementIndex + "," + VariantIndex);
 
     }
 }
 




 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using System;
 
 
 [Serializable]
 public struct ElementInfo{
     public string VariantName;
     public Sprite VariantIcon;
     public Material VariantMaterial;
 
 }
 
 [Serializable]
 public class ProductsYo{
     public string ItemName;
     public List<ElementInfo> Variants = new List<ElementInfo>();
 }
 

ui.jpg (23.9 kB)
scriptimage.jpg (42.4 kB)
Comment

People who like this

0 Show 0
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

Answer by sushillakra21 · Aug 29, 2018 at 09:06 AM

This is how I do it in C#, I write a handler for gameObject, handler implements interface such as OnTouch() or something, I have only one raycaster, which also has a hashtable, which is used to find the handler, you can write register/deregister methods in raycaster, hashtable maps GameObject InstanceID to Handler. now raycaster finds that a GameObject has been hit, it looks into hashtable if there's hadler for this game object if there is then calls OnTouch on handler.

Hope this helps.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

144 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

Related Questions

Change Object Material With a button C# 1 Answer

Using GUI and check what button was pressed 1 Answer

how to toggle on/off a gameobject with a GUI button ? 3 Answers

Help touch button 1 Answer

Create a button attached to a gameobject 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