Hi Unity Devs
Im hoping to get some deeper understanding of the best method for creating utility classes (unityscripts)
of which im going to need multiple instances of within another script. I have a working solution for my problem
but it requires me to set a reference … and alas im stuck now pondering if there is something im missing in the understanding of
unitys gameobject /component relationships and would love some input.
The issue Im trying to grasp here is unity event system working under the nested component system which only seems to trickle downwards
, let me give an example :
I have a “animal” GameObject , attached is a script “AnimalAI” , the controller.
In this script “AnimalAI” I would like to create two instances of an untility script with encapsulates state Management named “StateManager”, One to handle mood states and another to handle Acion states.
here is what ive all tested:
(option a) If I attach “StateManager” to “animal” GameObject and then within “AnimalAI” get a reference to use it for instancing with Instantiate
it recreates the “animal” GameObject as well , as the documentation explains … so this is not an option…
next test…
(option b) IF I create a variable in “AnimalAI” for “StateManager” I can not directly drag a “StateManager” script from project view onto this as the IDE will not permit this and has no drop action.
next test…
(option c) So I create an empty gameObject in the scene , add the “StateManager” script to this and now I drag this gameobject onto the variable reference
I created within"AnimalAI" which is datatyped to “StateManager” .
This works and I create two instances of it with ease and can target the “StateManager” variables directly , unity seems to bypass the actual gameobject scope (on which the “StateManager” script is attached)
when using Instantiate and now references the attached scripts.
BUT now if I want to use SendMessage or BroadcastMessage within “StateManager” instances , It seems to now remember the gameobject (on which it is attached) and the scope for receiving the message
the animal gameobject , is one up. Im baffeled here … documentation says for unity messages to work they need to be in the same scope of gameObject or a child etc…
but I cant attach them within the same scope as when calling Instantiate on one of them it recreates the whole parent gameobject as mentioned above?
In actionscript the holding instance that creates another registers itself for an event of that instance and this sets the scope.
(option d) So I solved this problem by the reference I mentioned in the start of this post , using (option c) In “StateManager” I created a variable named “targetGameObject”, and when
“AnimalAI” creates an instance of “StateManager” it sets “targetGameObject” to “gameObject” , which is then “animal” GameObject.
“StateManager” then calls targetGameObject.SendMessage or targetGameObject.BroadcastMessage and “AnimalAI” picks up the event as it is a child of “animal” GameObject.
how else do you get this child component and event system to work ??? …?
If you have read this far and understand what im pondering here (attached component and event scope logic ) , and have a suggestion
or opinion for what the best method would be to achieve this kind of OOP in unityscript , I have 6+ years experience with
actionscript and have some knowledge of other languages as well so feel free to explode with meaty answers here… im all ears.
(option e any thoughts?)
Thanks