It could be a childish sort of a question but i’m unable to understand or what I’ve understood from the lectures was that, we need to call a custom function in the start function for that custom function to execute. (Start or Update). So that’s why we called in ShowMainMenu, but then why we didn’t bothered for OnUserInput and how did it execute without a call in the Start function.
Hi Shahzad,
Great question, and well done for noticing this difference!
Unity has a specific order of executing functions (see the link below), if you create a method yourself, Unity doesn’t just execute it, it needs to be told to do so in one of it’s already executing methods, as you’ve identified, Start
, Update
etc.
With regards to OnUserInput
, you were given a project in this section which already has a lot of code under the hood, within this code, there is use of something called Reflection. It is used to interrogate the assembly (all of your code built into a .dll) and find every instance of a method called OnUserInput
, then, for each instance of this method it finds, in any script, it executes it.
The use of reflection is a little beyond the scope of the course, but, here’s a snippet from the code which executes it, just for reference - there’s no magic here
public void NotifyCommandHandlers(string input)
{
var allGameObjects = FindObjectsOfType<MonoBehaviour>();
foreach (MonoBehaviour mb in allGameObjects)
{
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
var targetMethod = mb.GetType().GetMethod("OnUserInput", flags);
if (targetMethod != null)
{
object[] parameters = new object[1];
parameters[0] = input;
targetMethod.Invoke(mb, parameters);
}
}
}
This is from Terminal.cs.
In the Awake
method, a delegate is registered for the onCommandSent
event, when that event occurs, the NotifyCommandHandlers
method is executed and subsequently, every instance of OnUserInput
.
Hope this helps
See also;
- Unity - Scripting API : Execution Order of Event Functions
Thanks for a quick reply Rob. For what I’ve understood from the explanation that you gave, is that you basically provided for the OnUserInput function in the Terminal.cs script file to execute separately from the entire code base.
Another reply that i received on the Facebook page is such and i quote “Some built-in methods are called by events like OnCollisionEnter. Any custom methods must be called in code”
Both replies differ as per my understanding, from each other. One says that yes it must be called in but because it was already coded in the Terminal.cs file that’s why the call isn’t needed. Other one, in my opinion tells that custom methods need to be called in, built in methods don’t.
Can you explain a bit. If my approach towards understanding it is right or?
is that you basically provided for the OnUserInput function in the Terminal.cs script file to execute separately from the entire code base
It’s not really separate from the code base. You have been given a working project which you can add to, e.g. adding your own code to, thus the code base is the same. It’s just that the execution of the method OnUserInput
is being called by the code you have already been given, and, because of it’s slightly more complex nature, isn’t covered/explored in the course.
Both replies differ as per my understanding, from each other. One says that yes it must be called in but because it was already coded in the Terminal.cs file that’s why the call isn’t needed. Other one, in my opinion tells that custom methods need to be called in, built in methods don’t.
Did you take a look at the link I provided above? That indicates the functions that Unity will handle, on your behalf, those which you don’t need to do anything to manage, e.g. Awake
, Start
, Update
etc.
If you create your own methods then yes, you need to manage those, and call them yourself. What is happening with the project you have been given is that an event has been created, and when that event is raised, it then manages the execution of the OnUserInput
method for you. OnCollisionEnter
is another example of this, an event is raised and this method is called. The only difference here, ignnoring the reflection, is that you get that one as part of Unity, where-as OnUserInput
has been added to this project for you.
Does this help?
Thanks a million. It’s all making sense now.
You’re very welcome