error CS0428: Cannot convert method group `OnClick' to non-delegate type `bool'.Consider using parents to invite the method

Asked 1 years ago, Updated 1 years ago, 62 views

I would like to do -=200 from (golds) when I click the button on Onclick.
error CS0428: Cannot convert method group OnClick' to non-delegate type pool'.Consider using parents to invite the method.
What should I do?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class shop:MonoBehavior {
    public int golds = 300;
    publicTextcoreText;
    /// What happens when you click a button?
    public void OnClick() {
        Debug.Log("Button click!");
    }

    void Start() {
        scoreText.text="golds:"+golds;
        if(OnClick){
            golds-=200;
        }
    }

    void Update() {
    }
}

c# unity3d

2022-09-30 17:43

1 Answers

How about the following methods?

<
Prepare a script to hold the gold variable and attach it to the GameObject.

public class MoneyBehavior:MonoBehavior {
    public int golds = 300;
}

<
shop.cs attaches to GamenObject with Button component.
For moneyBehavior, assign MoneyBehavior which you worked on in で

public class shop:MonoBehavior {
   public MoneyBehavior moneyBehavior;
   publicTextcoreText;

   void Start() {

       Button button=this.GetComponent<Button>();

       button.onClick.AddListener(OnClick);

       This.UpdateText();
   }

   void Update() {
   }


   /// What happens when you click a button?
   void OnClick() {
     Debug.Log("Button click!");
     moneyBehavior.golds -=200;
     This.UpdateText();
   }

   voidUpdateText(){
       scoreText.text="golds:"+moneyBehavior.golds;
   }       

}


2022-09-30 17:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.