- Home /
Why inpector keeps methods assigned when changed from public to private
When you make a public method, you can assign it in a unityEvent from inspector. But private ones can't be added or event shown in the list of listeners. But if you assigned a public method and then changed it to private. It can't be shown in inspector , however the last assigning will remains and the method can be fired. It's is now a private method. Why can another behavior invoke this method?
Perhaps this is the hidden function of Unity Engine to assign a private void to the button :)
This problem exists in versions 2020.3.0 and 2021.2.0 (this is alpha).
File a bug report if you think it's a bug with the editor.
I do think it is. I am reporting it, Thanks
Answer by hk1ll3r · Jun 02, 2021 at 06:05 PM
This may be a bug or by design.
The methods in events are serialized and written to disk with the monobehaviour that contains them. You can assume that it gets written as a string value: MyClassRef.MyCallback. If you delete the method for example, the editor cannot find that method anymore and shows a "method is missing" method. This is useful because if you introduce compilation issues, the method stays there and will start working again when you get rid of the compile issue.
In the case of public vs private, you would expect this scenario to work: 1- assign a public method to a Unity event like button click 2- mark the method private 3- mark the method public again That's why the editor doesn't clear the private method. It could however show an error message like the case where you delete the method.
The methods are accessed through reflection at runtime, so the modifier can be bypassed there. Again Unity could block this but they haven't.
Answer by sanabelapps · Jun 04, 2021 at 09:30 AM
I reported it as a bug and they replied to me that they know about this issue and decided it is by design. https://issuetracker.unity3d.com/issues/button-onclick-list-is-not-modified-if-the-target-methods-accessibility-is-set-to-private
Your answer
Follow this Question
Related Questions
When to use public or private variables for the inspector? 2 Answers
Inspector cannot find script instance 1 Answer
C# Return Type Error? 1 Answer
Multiple Cars not working 1 Answer
Float sets itself to zero 1 Answer