C# 通过单击按钮C减少变量#

C# 通过单击按钮C减少变量#,c#,unity3d,user-interface,C#,Unity3d,User Interface,我想通过单击按钮减少一个值,但它不会停留在并返回到开头的值, 这是我的代码,我应该添加或更改什么?有人能帮我吗 非常感谢 public class LifeShow : MonoBehaviour { public GameObject Life; //where the variable will be decreased from public int Lifevalue; public string text; public Button

我想通过单击按钮减少一个值,但它不会停留在并返回到开头的值, 这是我的代码,我应该添加或更改什么?有人能帮我吗

非常感谢

public class LifeShow : MonoBehaviour
{
    public GameObject Life; //where the variable will be decreased from
        
    public int Lifevalue;
    public string text;
    public Button returns;
    // Start is called before the first frame update
    void Start()
    {
        Lifevalue = 1;

        Life.GetComponent<Text>().text = "Life : " + Lifevalue;
    }

    // Update is called once per frame
    // clicking the button and the lifevalue variable decreases
    void Update()
    {
        Button btn = returns.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);



        void TaskOnClick()
        {
            Lifevalue-- ;

            Life.GetComponent<Text>().text = "Life : " + Lifevalue;

            
            

        }
    }
}
公共类生活方式:单一行为
{
public GameObject Life;//变量将从何处减少
公共生命价值;
公共字符串文本;
公共按钮返回;
//在第一帧更新之前调用Start
void Start()
{
寿命值=1;
Life.GetComponent().text=“Life:+Lifevalue;
}
//每帧调用一次更新
//单击按钮,lifevalue变量将减小
无效更新()
{
按钮btn=返回.GetComponent();
btn.onClick.AddListener(TaskOnClick);
void TaskOnClick()
{
生命价值--;
Life.GetComponent().text=“Life:+Lifevalue;
}
}
}

您可以将本地方法
void TaskOnClick()
更改为公共方法,方法是将其置于update之外并使用
public
关键字,然后您可以在unity中添加OnClick

现在点击“+”按钮并拖放脚本附加到的对象。然后你可以找到这个

选择你的功能,你就准备好了。您不需要通过脚本来执行此操作。 如果要将其限制为不低于0,可以在脚本中添加If条件

if(LifeValue > 0)
{
  LifeValue--;
  // showing text
}

它将防止低于0。

不是Mono的专家,但首先,似乎在每个帧上都附加了一个事件处理程序,这是不好的。代码应为:

public class LifeShow : MonoBehaviour
{
    public GameObject Life; //where the variable will be decreased from
        
    public int Lifevalue;
    public string text;
    public Button returns;
    // Start is called before the first frame update
    void Start()
    {
        Lifevalue = 1;

        Life.GetComponent<Text>().text = "Life : " + Lifevalue;
        Button btn = returns.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void TaskOnClick()
    {
        Lifevalue-- ;
        Life.GetComponent<Text>().text = "Life : " + Lifevalue;
    }
}
公共类生活方式:单一行为
{
public GameObject Life;//变量将从何处减少
公共生命价值;
公共字符串文本;
公共按钮返回;
//在第一帧更新之前调用Start
void Start()
{
寿命值=1;
Life.GetComponent().text=“Life:+Lifevalue;
按钮btn=返回.GetComponent();
btn.onClick.AddListener(TaskOnClick);
}
//每帧调用一次更新
无效更新()
{
}
void TaskOnClick()
{
生命价值--;
Life.GetComponent().text=“Life:+Lifevalue;
}
}
第二,很不清楚Lifevalue和Life Game Object之间有什么区别,你能告诉我吗

编辑:正如有人在评论中所说,这段代码实际上不足以给出好的提示,你能展示生命对象的代码吗?那会很有帮助。

  • 单击
    +
    添加侦听器
  • 将侦听器对象拖动到插槽(Life)
  • 生活脚本中的选择函数(修改)
  • 设置参数值
    1
    /
    -1
    /
    -99

[不允许使用MultipleComponent]
公共阶层生活:单一行为{
公共事件系统。操作已更改;//具有INT属性的事件
私人国际公寓;
public int-Amouth{//this in属性
get=>\u amouth;
//设置Amouth时,更改\u Amouth并调用“Changed”事件
设置{
if(_amouth!=值){
_amouth=值;
已更改?.Invoke(_amouth);
}
}
}
私有void开始(){
Amouth=1;
}
公共无效修改(整数){
Amouth+=数量;
}
}

[不允许使用MultipleComponent]
[RequiredComponent(typeof(Text))]
公共类LifeView:MonoBehavior{//将组件添加到文本对象
[SerializeField]私人生活_-Life;//在inspector窗口上设置
私人文本(u Text),;
公共int{get;私有集;}
私有void-OnEnable(){
_text=GetComponent();
UpdateText(_life.Amouth);
_life.Changed+=UpdateText;//订阅life事件
}
私人无效不可撤销(){
_life.Changed-=UpdateText;
}
私有void UpdateText(int-amouth){
_text.text=“生活:”+amouth;
}
}

我试图在代码中的变量附近添加描述,但我不确定应该如何添加,因为我是csharp和论坛的新手:/n您确定要为每一帧添加
btn.onClick.AddListener(TaskOnClick)
?通常,您需要注册一次事件处理程序。您应该将TaskOnClick函数移到update函数的外部。除此之外,您不需要仅为该功能进行更新。创建TaskOnClick函数作为单独的函数,更新函数的前两行应该在start函数的内部。这应该行得通。我希望每次当生命值大于0时,在点击按钮时减少生命值,我应该在点击按钮时使用其他东西吗?是的,我对Csharp非常陌生,我正在努力学习。谢谢你的帮助非常感谢我会尝试的,life gameobject是一个文本对象,lifevalue是我希望出现在下一个文本对象附近的变量。life Evalue是一个整数,life是一个游戏对象。很简单,我想我现在看到了。同样,不是Mono专家,但“返回”字段在哪里初始化?你调用returns.GetComponent(),这不是抛出了一个异常(null)?@I-am-developer-9这很清楚:)我只是不知道Life对象是做什么的/有什么用他用它来改变Life对象@Pitthanks的文本组件的文本很多,谢谢你的回答,它确实有效,但我的问题是,当我返回到开始场景时,生命值返回到1而不是0。我想我应该更新它。我不明白你的意思抱歉..我希望按下按钮时整数保持不变,当我按下按钮时,它会改变整数,但随后它会返回到开始时的值,所以它不会减小。这可能会导致重新启动场景或重新加载。是的,如果重新启动,它将被重置,因为它没有保存
[DisallowMultipleComponent]

public class Life : MonoBehaviour {

    public event System.Action<int> Changed; // event with INT attribute
    private int _amouth;

    public int Amouth { // this in property
        get => _amouth;
        // when you set Amouth, then change _amouth and invoke "Changed" event
        set {
            if (_amouth != value) {
                _amouth = value;
                Changed?.Invoke(_amouth);
            }
        }
    }

    private void Start () {
        Amouth = 1;
    }

    public void Modify (int number) {
        Amouth += number;
    }
}
[DisallowMultipleComponent]
[RequireComponent(typeof(Text))]

public class LifeView : MonoBehaviour { // Add Component to text object

    [SerializeField] private Life _life; // set on inspector window
    private Text _text;

    public int LineAmouth { get; private set; }

    private void OnEnable () {
        _text = GetComponent<Text>();
        UpdateText(_life.Amouth);
        _life.Changed += UpdateText; // subscribes to Life event 
    }

    private void OnDisable () {
        _life.Changed -= UpdateText;
    }

    private void UpdateText (int amouth) {
        _text.text = "Life : "+amouth;
    }
}