Why are the "OnPress" functions public?

Hi all,

First time posting here. This should maybe be in the qa for the last video but I am just noticing it now. Why are the OnPress functions public? They appear to work without making them public.

Thank You,

Mikyle Crawford

Hello Mikyle, welcome to the community :slight_smile:

It is absolutely fine to ask questions on the community forum, its a very supportive place. The Q&A has dedicated student instructors who will aim to resolve your queries or will escalate to an instructor where necessary. Both the forum and the Discord channel provide more peer support, e.g. other students or people who give up some time to help you :slight_smile:

Regarding your query, in order for those methods to be visible by other classes they need to use the public access modifier. In this specific case, you wire these methods up to the OnClick events for the buttons within the scene I believe. If they were left as private, they would only be available within the class itself.

Hope this helps :slight_smile:

Hi Rob,

Thank you for the prompt reply. I tried removing the public modifier and the onclick still appears to be working. I understand what you are saying about scope. Now I wonder why onclick seems to have access despite the lack of public.

Mikyle

Hey,

You’re very welcome.

So, to confirm, you have your script attached to a GameObject in the scene, and you then dragged that GameObject to the object field of the OnClick event for the button - not just dragged the script from the Assets folder?

Assuming so, once you’ve made the change to the script removing the public access modifier those methods shouldn’t be available to the OnClick event, even having been wired up before. My expectation would be for them to be removed from the OnClick event in the inspector, ideally showing an error about a missing method/member or throw an error at least when you run the game.

Could you pop a screenshot up or better still, share your project files so I could take a quick look? Seems odd.

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

Interesting. The option to select it is no longer there but its still hooked up.

Sounds like a bit of an area Unity could be enhanced then. I would imagine if you were to run the game it would create an error, as you would have a wired method which doesn’t have the relevant access level for the button to access it.

That’s what I am say though. I even built the game like this and it is working.

I mean there is something else with it going up to like 1004 but that something else.

Can you share your project files so I can take a look?

It wont let me upload here is a link to it on google drive.
https://drive.google.com/open?id=1GDC2KfRpD8o4897m6aYKR8O9ysjppd_L

Hi,

I’ve had a look and can see exactly what you mean and the behaviour is replicated here too.

If you click on the circle selector next to the object field for the OnClick event and re-select Gameplay, or drag the object back across, it recreates the references, in doing so it is then no longer possible to select the methods which are private.

All I can think of here is that those references are not getting updated in that object, perhaps some caching takes place in the background, what surprises me the most is that when you run the game this still works, and in fact if you build the game this still works.

I tried the following;

  • added private access modifier in front of the method names to see if they may be forced to disappear - nope.
  • removed one method completely, this forced a recompile and the OnClick event then displayed an error message where the method was previously listed - this suggested a recompile may be the solution - however, undoing that change, so that both private methods were back in the scripts again forced other recompile and the OnClick event was then re-wired as it was original - and worked!

I would definitely suggest this is a bug within Unity.

I’ve raised this on the Unity forum to see what feedback it gets before perhaps raising it on the Unity Issue Tracker.

Cool thanks for letting me know I shall continue on with the course and keep an eye on that thread.

1 Like

No problem, it will be interesting to see if anyone responds, if not, I’ll add it to the Unity Issue Tracker and see what happens.

Out of curiosity for the issue where if I keep clicking higher and the number then goes over my max. I noticed it looks like this is because my min value goes over my max value. my solution was to do this with my on press higher

void OnPressHigher()
{
    if(guess + 1 <= max)
    {
        min = guess + 1;
        NextGuess();
    }
}

is there a better way to do this? perhaps enforce the max min values in random.range?

… aside from forgetting the public again haha

1 Like

One thing you could consider doing, which might make the game look a bit better too, would be to disable the buttons that are not relevant.

So for example, when you recalculate the guess, via NextGuess, determine at that stage whether either the Lower or Higher buttons should be disabled and then disable them. This should prevent the issue because you wouldn’t be enabling the player to click the buttons in the first place.

Regarding disabling them, you could either not show them at all, or, if you wanted to do something a bit more complicated, change them so that they were perhaps greyed out, remove the OnClick events (remembering you’ll need the ability to re-add them if the player plays again), you could even play an audio effect if they try to click on a button that is disabled.

1 Like

Privacy & Terms