简体   繁体   English

Unity3d c#。 onClick.AddListener()工作一次

[英]Unity3d c#. onClick.AddListener() works once

There's a void in the script, which reloads Canvas. 脚本中有一个空白,它重新加载Canvas。 The Canvas contains the Button object. Canvas包含Button对象。 I attach the reload void with the script. 我用脚本附加了reload void。

using UnityEngine;
using UnityEngine.UI;

public class Trashscript : MonoBehaviour {

   public GameObject Temp;
   private GameObject Active;
   int Counter;

   void Start()
   {
       Menu();
   }

  void Menu()
   {
       Counter++;

       if (Active != null) Destroy(Active);
       Active = Instantiate(Temp);

       GameObject.Find("Text").GetComponent<Text>().text = Counter.ToString();
       GameObject.Find("Btn").GetComponent<Button>().onClick.AddListener(Menu);
   }
}

First call (from Start ) works fine the "Text" element shows "1". 第一次调用(从Start )工作正常,“Text”元素显示“1”。 After clicking the "Btn" Canvas reloads, but there isn't any "Text", and "Btn" doesn't work (onClick event do nothing). 单击“Btn”Canvas重新加载后,但没有任何“文本”,“Btn”不起作用(onClick事件什么都不做)。

Help. 救命。

I can't tell what your code is doing but each time the Button is clicked, Menu function is called which leads to GetComponent<Button>().onClick.AddListener(Menu) being called again. 我无法分辨你的代码在做什么,但每次单击Button ,都会调用Menu函数,这会导致再次GetComponent<Button>().onClick.AddListener(Menu)

You have to move GetComponent<Button>().onClick.AddListener(Menu) out of the Munu function and into the Start() function. 您必须将GetComponent<Button>().onClick.AddListener(Menu)移出Munu函数并进入Start()函数。 You also have to un-register to the Button event in the OnDisable() function. 您还必须在OnDisable()函数中取消注册Button事件。

Below is what your code should look like: 以下是您的代码应如下所示:

Button button;

void Start()
{
    button = GameObject.Find("Btn").GetComponent<Button>();
    button.onClick.AddListener(Menu);
}

void OnDisable()
{
    button.onClick.RemoveListener(Menu);
}

void Menu()
{
    ///Put Your Button click code here

}

我解决我的问题与改变GameObject.Find("")Active.transform.Find("")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Unity OnClick.AddListener 停止工作 - Unity OnClick.AddListener Stopped Working C#Unity,“更改数组”按钮颜色仅在添加OnClick.Addlistener时受影响的最后一个数组 - C# Unity, Changing Array button colour only last array being affected when added OnClick.Addlistener 如何在一帧中对多个游戏对象使用“OnClick.AddListener” - How to use “OnClick.AddListener” on multiple GameObjects in one Frame Unity3D数据库在编辑器中检索,但在编译C#时不检索。 在其他地方取回罚款 - Unity3D database retrieves in editor but not when compiled C#. Retrieves fine in other places 按钮Onclick无法使用公共功能-UNITY3D C# - Button Onclick not working with public functions - UNITY3D C# foreach 循环只迭代一次? C# Unity3D - foreach loop iterating only once? C# Unity3D 发送 UDP 消息有效,接收无效 - Unity3d 上的 C# - Sending UDP messages works, receiving not - C# on Unity3d Unity3D C#。 使用transform.RotateAround()函数,同时保持两个对象之间的距离不变 - Unity3D C#. Using the transform.RotateAround() function while keep the distance between two objects constant 将新脚本附加到unity3d C#中的按钮onclick事件 - Attach new script to button onclick event in unity3d c# Windows 8/10上的Unity3D C ++非托管DLL崩溃,适用于7 - Unity3D C++ unmanaged DLL crash on windows 8/10, works on 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM