UnityEvent using a GameObject's script method versus a prefab's script method
Hello
Let's say I have in my scene a GameObject called "MyScriptContainer" with a C# script called "MyScript" attached that has a public void DoStuff() {...}, then I have a UI button and on it's OnClick event, I drag&drop "MyScriptContainer" in and select "MyScript" > "DoStuff". Now if I click the button, the "DoStuff" method will be called, basic stuff...
If I prefab the MyScriptContainer with it's C# script by drag&drop in Project tab. And on my button I use the prefab for the event handler. What I mean here is instead of drag&drop the instance of MyScriptContainer from the Hierarchy tab, I drag&drop the prefab from the Project tab. Everything still works! Even if I remove the instance from the hierarchy. But why/how?
I'm following an online course on Udemy and I had to do something like that. I'd like to understand what is happening here.
Does Unity do a search for an instance of that prefab in the scene and then use it? And if it can't find one, then create an instance...?
Or does it instantiate a private version of this prefab for my button? If I do this again with a 2nd button, will I get 2 separate MyScriptContainer and MyScript instances?
Thank you
Your answer
Follow this Question
Related Questions
What is Casting in Unity ? 1 Answer
Save gameobject from imported mesh (with imported texture) as prefab/resource at runtime. 0 Answers
Create gameobject in hierarchy on Unity refresh but before Play mode 0 Answers
Can you turn a prefab/gameobject into a mesh? 0 Answers
Why does GameObject.FindWithTag() get a wrong instance? 2 Answers