The problem of calling the function from a class in the button component in Unity

Hello to all.
My Unity is having trouble getting a function from a class in a code
When I want to give a code to the button component, there is no problem, but when I want to select a function from that code, it does not show me those functions at all.

Please help me :slight_smile:

my code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Rules : MonoBehaviour
{
    public Transform player;
    public GameObject lose_menu;
    
    public void Restart_Game()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
        Time.timeScale = 1;
    }

    public void Exit_Game()
    {
        Application.Quit();
    }

    public void Next_level()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }

    void Update()
    {
        if(player.position.y < -10)
        {
            Time.timeScale = 0;
            lose_menu.SetActive(true);
        }
        else
        {
            Time.timeScale = 1;
        }
    }
}

3 Answers

3

You are incorrectly dragging a script asset into the UnityEvent, instead of the instance.

thank you . it worked

โ€“

Is your Rules script attached to an object? You canโ€™t run an independent script from a button press. You need to give it a reference to an object, then select one of the public methods present on that object.

Yes . The โ€œrulesโ€ component is attached to a GameObject