C# GetComponent要求请求的组件';按钮';源于单一行为或组件或是接口
我几乎失去了所有的希望。 我想做的基本上是实例化一个预置,它基本上是一个带有图像的按钮,一旦实例化,我想设置一个要调用的onClick函数,但它总是告诉我这个错误: GetComponent要求请求的组件“按钮”派生自MonoBehavior或组件,或者是接口 我不明白我做错了什么,这是我的代码:C# GetComponent要求请求的组件';按钮';源于单一行为或组件或是接口,c#,unity3d,C#,Unity3d,我几乎失去了所有的希望。 我想做的基本上是实例化一个预置,它基本上是一个带有图像的按钮,一旦实例化,我想设置一个要调用的onClick函数,但它总是告诉我这个错误: GetComponent要求请求的组件“按钮”派生自MonoBehavior或组件,或者是接口 我不明白我做错了什么,这是我的代码: using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using U
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class InventoryCoordinator : MonoBehaviour
{
// Start is called before the first frame update
public GameObject prefabPocion;
void Start()
{
GameObject panelInventuario = GameObject.FindGameObjectWithTag("Hotbar");
if (panelInventuario)
{
GameObject primerSlot = panelInventuario.transform.GetChild(0).gameObject;
GameObject segundoSlot = panelInventuario.transform.GetChild(1).gameObject;
GameObject tercerSlot = panelInventuario.transform.GetChild(2).gameObject;
var newPocion = Instantiate(this.prefabPocion, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity);
this.SetAndStretchToParentSize((UnityEngine.RectTransform)newPocion.transform.transform, (UnityEngine.RectTransform)primerSlot.transform.transform);
var button = GameObject.FindWithTag("PotionFreeze").gameObject.GetComponent<Button>();
button.clicked += manguito;
//newPocion.transform.parent = primerSlot.transform;
}
}
// Update is called once per frame
void Update()
{
}
public void manguito()
{
Debug.Log("full maracaton");
}
public void SetAndStretchToParentSize(UnityEngine.RectTransform _mRect, UnityEngine.RectTransform _parent)
{
_mRect.anchoredPosition = _parent.position;
_mRect.anchorMin = new Vector2(1, 0);
_mRect.anchorMax = new Vector2(0, 1);
_mRect.pivot = new Vector2(0.5f, 0.5f);
_mRect.sizeDelta = _parent.rect.size;
_mRect.transform.SetParent(_parent);
_mRect.localScale = new Vector3(1f, 1f, 1f);
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.ui元素;
公共类清单协调员:MonoBehavior
{
//在第一帧更新之前调用Start
公共游戏对象预制;
void Start()
{
GameObject panelInventuario=GameObject.FindGameObjectWithTag(“热栏”);
如果(面板清单)
{
GameObject primerSlot=panelInventuario.transform.GetChild(0).GameObject;
GameObject segundoSlot=panelInventuario.transform.GetChild(1).GameObject;
GameObject tercerSlot=panelInventuario.transform.GetChild(2.GameObject);
var newPocion=实例化(this.prefapocion,新向量3(transform.position.x,transform.position.y,transform.position.z),四元数.identity);
此.SetAndStretchToParentSize((UnityEngine.RectTransform)newPocion.transform.transform,(UnityEngine.RectTransform)primerSlot.transform.transform);
var button=GameObject.FindWithTag(“PotionFreeze”).GameObject.GetComponent();
button.clicked+=manguito;
//newPocion.transform.parent=primerSlot.transform;
}
}
//每帧调用一次更新
无效更新()
{
}
公共空间
{
Log(“完整马拉卡顿”);
}
public void SetAndStretchToParentSize(UnityEngine.RectTransform\u mRect,UnityEngine.RectTransform\u父对象)
{
_mRect.anchoredPosition=\u parent.position;
_mRect.anchorMin=新向量2(1,0);
_mRect.anchorMax=新向量2(0,1);
_mRect.pivot=新矢量2(0.5f,0.5f);
_mRect.sizeDelta=\u parent.rect.size;
_mRect.transform.SetParent(_parent);
_mRect.localScale=新矢量3(1f、1f、1f);
}
}
我已经试着得到游戏对象并向前或向后施放,但什么都没有,我需要你们的帮助!提前感谢您遇到此问题是因为您使用的是来自
UnityEngine.UIElements
的按钮,afaik还不支持ingame按钮(用于自定义编辑器)。改用UnityEngine.UI
,并替换:
button.clicked += manguito;
与
成功了!你知道为什么vscode会导入UIME元素而不是.UI吗?为了解决这个问题,我花了5个小时的时间,即使现在有了你建议的导入,它也会以红色突出显示它们,尽管在unity engine上编译得非常完美=/我没有使用VSCode,所以我真的帮不上忙。您可以检查您的设置是否正确:您使用了错误的按钮。改用UnityEngine.UI
名称空间
button.onClick.AddListener(manguito);