I’m having trouble wiring up the business logic to a prefab GameObject.
When instantiating the prefab, I attach the meta data as follows:
GameObject go = Instantiate (monsterPrefab, monsterPosition, Quaternion.identity);
MonsterObject monsterObj = go.AddComponent(typeof(MonsterObject )) as MonsterObject ;
// monsterObj is good
monsterObj.property = 1000;
Then when the user clicks on the monster in the game, I’d like to access the MonsterObject. In the monsterPrefab, I have an EventTrigger with a PointerClick event that calls a script and method “Selection.MonsterSelected” callback. A separate “Selection.cs” script is contained in an otherwise empty GameObject (“Selection”) that I dragged into the EventTrigger to access the Selection::MonsterSelected() callback from the drop down menu.
When MonsterSelected is called, I assume that code is running a component attached to my instantiated GameObject. From that callback, I try to access the MonsterObject component, but get a null pointer:
// monsterObj is null
MonsterObject monsterObj = GetComponent (typeof(MonsterObject)) as MonsterObject;
Does anyone know how I can attach script containing business logic and metadata to an instantiated prefab, and then access that object after clicking on it?
Spent more time on this one, but I’m still confused where my EventTrigger callback lives.
The InstanceID of my GameObject, go, above, is something like -11332, similar to other GameObjects and component ID’s. However from my Selection.cs script PointerClicked callback, GetComponent and GetComponentInParent are not finding the MonsterObject.
Does my EventTrigger just live in a Prefab, from which I cannot access any instantiated components? If so, how do I reference the instantiated GameObject and it’s components from an EventTrigger?
Just wanted to summarize my results on this in case anyone else runs into the same problem. I did’t represent the whole MonsterObject above, but basically, it was a base class that derived from MonoBehavior, and was subclassed by different monster types.
I ended up trashing that code and starting over before understanding where my problem was, but my new solution for attaching metadata to a GameObject works well. My new design is to represent each creature as two classes:
- A Unity class deriving from MonoObject that only contains methods to interact with the scene graph, such as selection and animation.
- A business logic parent class that does not derive from MonoObject, with derived classes for specific creature types to handle game state management (AI, combat, etc)
The unity class has a member variable that references the second business logic class, and comes pre-attached as a component to the prefab so that when I Instantiate() the prefab and get back a game object, the Unity class is already created. So after the Instantiate, I call “new” on the second business logic class and store the reference to it in the unity class, so the animation and selection routines can access game state if necessary.