Run a function on a Panel from a script on a prefab programically

I have a prefab with a script attached which handles a mouse click and a trigger enter/exit.

I want to be able to call a function that is on a panel, so it occurred to me to get the script component on the gameobject, but I don’t know how to access the functions in the script.

GameObject main;
    main = gameObject.GetComponent<Maintanence> (this is the name of the script)

Because one script is on a prefab and the other is on a panel, I can’t use the “UI way” to hook them together.
Ultimately I want to delete an item from the database by clicking on it. Then have the list updated with the current information and instantiated through the prefab. Does that make sense?

Im not sure if I understand your question. When you run a panel, which is just a UI element, you are probably running it from a gameobject that is in your scene. The prefab will also have to be in the scene if you want to access it.

Running functions in other scripts always seems to be something people can’t get their head around. It’s actually not that complicated.

Let’s say we have a script (class) called Janitor. The Janitor class will have some values (variables) like: His name, his age and maybe his favorite bucket to use. The Janitor can also do things (functions/methods) like: sweeping and whistling.

As a (unity)class this would look like this:

public class Janitor : MonoBehaviour{

    public string name;
    public int age;
    public string favoriteBucket;

    public void Sweep()
    {
        Debug.Log("I'm Sweeping");
    }

    public void Whistle()
    {
        Debug.Log("I'm Whistling");
    }

}

You could place this class on an object and put it in your scene. But how to make the Janitor sweep the floor?

There are multiple ways to make a script access another script. Lets say we have a school class:

public class School : MonoBehaviour{

//You could simply make a public variable and drag the Janitor in, in the inspector.
public Janitor janitor;

//Instead of dragging it in by hand, you could look for the object in the scene:
void Start(){

    //By name, your object must be named "Janitor"  in the scene
    janitor = GameObject.Find("Janitor").GetComponent<Janitor>();

    //By tag, your object must have the correct tag (in this case "Janitor").
    //Make sure to have only one object with this tag.
    janitor = GameObject.FindWithTag("Janitor");

}
}

Now that we found our Janitor class, we can call functions on it:

janitor.Sweep();
janitor.Whistle();

So now that we know the basics, lets apply it to your situation:

Let’s say your Maintenance class has a function called Repair().
To call this function from another gameobject:

//Drag in from inspector:
public Maintenance main;

//Or find the maintenance class in the scene by name:
Maintenance main = GameObject.Find("MaintenanceObject").GetComponent<Maintenance>();

//Call the function
main.Repair();

If this is not the answer to your question, i just need some extra information and we’ll get it working.

What I think you’re saying is that in your scene there is a UI panel with an attached MonoBehaviour called maintenance, and a bunch of instantiated UI prefabs, and you want to have them execute a function on Maintenance when clicked. The way I would do this would be to create a static Maintenance variable in the Maintenance class, so any of the prefabs could use a script and access it.

    private Maintenance instance;
    public Maintenance Instance
    {
        get
        {
            if(instance == null)
            {
                instance = FindObjectOfType<Maintenance>();
            }
            return instance;
        }
    }

    private void Awake()
    {
        instance = this;
    }

Then, when you want to execute a method on the Maintenance instance attached to the panel, you could call something like this:

    public void OnPointerUp (PointerEventData eventData) 
    {
        Maintenance.Instance.ExecuteMyLogic(instantiatedPrefabData);
    }