Why does the Replay button not see any of the GameManager functions?

I’ve created my button, and created my GameManager script. I added a public OnReplayLevel function to the script, saved it, and then tried to add it to the On Click list for my button. Unity tells me “No Functions”. I know there is a workaround where I can use my GameManager game object from the hierarchy instead of the script, but this adds a bunch of other methods and feels messy. Such a basic thing should not need a mystery workaround. So. Any idea why this is happening?

Hi vkhavin,

Welcome to our community! :slight_smile:

Is the OnReplayLevel method public? Did you save your script? Sometimes, the script does not get saved properly even if you pressed Ctrl + S. Try to save it again.


See also:

Hi Nina,

Thanks for the quick reply! Yes, the method is public. I’ve tried this a number of times now. I’ve recreated the script, and I’ve also recreated the button. I tried using a prefab button from an earlier chapter, and also tried creating a brand new button, and have consistently seen this behavior. I tried restarting Unity (I’ll admit I haven’t tried to reboot the whole machine but that seems unlikely to solve anything.)

Here is a screenshot of the script:

And here is the Unity editor:

The one difference I did notice is that my GameManager script does not get the fancy icon, but that difference in behavior may be due to a difference in version of Unity. Or is that indicative of a problem?

Thanks again!

Please take a look at this field:

image

A common mistake is to assign the script. That does not work. You have to assign the script to a game object. Then assign the game object to the field. Have you already tried that?

Ah. Yes, I had, though it brought with it a lot of other functions which I thought was indicative of an issue. If this is the expected behavior, then great! For some reason, I thought it was accomplished more cleanly earlier but it seems I am mistaken. Thank you very much!

Yes, that’s the expected behaviour. When you assign a script to a game object, Unity automatically creates a component object. This component object provides certain features in Unity. The script itself is ‘nothing’. And that’s the reason why we never do anything with a script file in the editor apart from creating a component. Then we connect the components.

In the case of the Button component, we connect the Button component with the GameManager component.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms