I've lost all hope, almost. What im trying to do is basically instantiate a prefab I have which is basically a button with an image, and once instantiated, I want to set a onClick function to be called, but it keeps telling me this error:
GetComponent requires that the requested component 'Button' derives from MonoBehaviour or Component or is an interface
I dont understand what im doing wrong, here is my code:
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);
}
}
I've tried to get the gameObject and cast it, backwards and forwards, and nothing, I need your help guys! Thanks in advance