- Home /
Button OnClick doesn't refer to a function in a script, it just says "No Function"
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?
Answer by actionjdjackson · Apr 29, 2020 at 05:56 PM
Here's how it looks when I pick GiveInventoryItem as the script object. Only a "string" to enter.
Answer by yummy81 · Apr 30, 2020 at 05:36 AM
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");
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to get all objects instantiated from a single prefab? 1 Answer