Dynamics buttons OnClick not working

Hi,

I use this Json to create dynamics buttons, but when I try to add listener on it, function “PlayVideoOrPhoto360” never called:

private void Processjson(string jsonString)
        {
            jsonvale = JsonMapper.ToObject(jsonString);
          
            model.ID = new ArrayList ();
            model.Icon = new ArrayList ();
            model.Name = new ArrayList ();
            model.UrlImage360 = new ArrayList ();
            model.UrlVideo360 = new ArrayList ();
            model.Info_en = new ArrayList ();
            model.Info_fr = new ArrayList ();
            model.Sound = new ArrayList ();
            model.UrlImages = new ArrayList ();
            model.PositionX = new ArrayList ();
            model.PositionY = new ArrayList ();
            model.PositionZ = new ArrayList ();
          
            Debug.Log(jsonvale["items"].Count);
          
            for(int i = 0; i<jsonvale["items"].Count; i++)
            {
                model.ID.Add(jsonvale["items"][i]["ID"].ToString());
                model.Icon.Add(jsonvale["items"][i]["Icon"].ToString());
                model.Name.Add(jsonvale["items"][i]["Name"].ToString());
                model.UrlImage360.Add(jsonvale["items"][i]["UrlImage360"].ToString());
                model.UrlVideo360.Add(jsonvale["items"][i]["UrlVideo360"].ToString());
                model.Info_en.Add(jsonvale["items"][i]["Info_en"].ToString());
                model.Info_fr.Add(jsonvale["items"][i]["Info_fr"].ToString());
                model.Sound.Add(jsonvale["items"][i]["Sound"].ToString());
                model.UrlImages.Add(jsonvale["items"][i]["UrlImages"].ToString());
                model.PositionX.Add(jsonvale["items"][i]["PositionX"].ToString());
                model.PositionY.Add(jsonvale["items"][i]["PositionY"].ToString());
                model.PositionZ.Add(jsonvale["items"][i]["PositionZ"].ToString());
              
                InstantiateHotspots(i);
            }  
        }
      
        private void InstantiateHotspots(int i)
        {
            hotspot.name = model.ID[i].ToString();
            hotspot.GetComponentInChildren<TextMeshProUGUI>().text = model.Name[i].ToString();
            if(GetInt(model.Icon[i].ToString(), 0) == 1){
                hotspot.GetComponentInChildren<SpriteSwapper>().m_swapped = true;
            } else {
                hotspot.GetComponentInChildren<SpriteSwapper>().m_swapped = false;
            }
            Button tempButton = hotspot.GetComponent<Button>();
            tempButton.onClick.AddListener(() => PlayVideoOrPhoto360(model.UrlVideo360[i].ToString()));
            hotspot.SetActive(true);

            Instantiate(hotspot, new Vector3(GetFloat(model.PositionX[i].ToString(),0), GetFloat(model.PositionY[i].ToString(),0), GetFloat(model.PositionZ[i].ToString(),0)), Quaternion.identity).transform.SetParent(this.transform);
          
        }
      
        public void PlayVideoOrPhoto360(string value){
            Debug.Log("PlayVideo: "+value);
        }

All hotspot was correctly created, positionned etc… but AddListener not work !
Any clue ?

Thanks

Looks to me you are applying your changes to a prefab instead of the newly instantiated object.

GameObject newSpot =  Instantiate(hotspot, new Vector3(GetFloat(model.PositionX[i].ToString(),0), GetFloat(model.PositionY[i].ToString(),0), GetFloat(model.PositionZ[i].ToString(),0)), Quaternion.identity).transform.SetParent(this.transform);

You should do the above and then apply all things to the newSpot.

so…

newSpot.name = “name”;
newSpot.GetComponent().onClick.AddListener(() =>…

that sort of thing.

Note: I’m just guessing the rest is correct, you just generally don’t apply changes like this to a prefab. You generally create the copy and then modify the copy.

1 Like

Thanks but I get this error : error CS0039: Cannot convert type void' to UnityEngine.GameObject’ via a built-in conversion

Didn’t see the SetParent call. You can choose a parent in the instantiate call, so just change it to

GameObject newSpot =  Instantiate(hotspot, new Vector3(GetFloat(model.PositionX[i].ToString(),0), GetFloat(model.PositionY[i].ToString(),0), GetFloat(model.PositionZ[i].ToString(),0)), Quaternion.identity, transform)

Nice, works fine now :slight_smile: thanks for the tip’s Brathnann !