I’ve successfully created a custom button that “presses” correctly, but there’s no options for the OnClick function - it just says “No Function” - I can’t get it to work. I want to reference a function in a script attached to the same button object called GiveInventoryItem. Any ideas?
As I look at your images, I suppose that you should drag and drop your gameobject Button into the slot. I mean, drag Button gameobject from the hierarchy, and drop it into the slot. This should be enough. You dragged and dropped only script.
The general idea is that you have to create a gameobject, then attach “GiveInventoryItem” script to the gameobject. Now, drag and drop the gameobject with the script (not only script) into the slot in your Button. Remember to make the function public.
You can also do it programmatically like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class GiveInventoryItem : MonoBehaviour
{
void Awake() => GetComponent<Button>().onClick.AddListener(YourFunction);
public void YourFunction() => print("YourFunction");
}
Here’s how it looks when I pick GiveInventoryItem as the script object. Only a “string” to enter.
I’m following a tutorial on Udemy and ran into a similar problem, although I selected the game object from the list after clicking the radio button. I wasn’t seeing the script component attached to the object. Weirdly, the fix for me was to just drag and drop the object from the Hierarchy rather than select from the list.
Thanks to yummy81 for the suggestion of drag and drop.