Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: BeNamed on April 04, 2011, 06:33:28 AM

Title: [solved] Send event in script
Post by: BeNamed on April 04, 2011, 06:33:28 AM
When I try to send an event in my script like this:
behavior.Fsm.Event("event");
I try to do it like here: https://hutonggames.fogbugz.com/default.asp?W329 (https://hutonggames.fogbugz.com/default.asp?W329)
But this is the result...
It gives an error: 'object reference not set to an instance of an object'.

Is there something I forgot?

thank
Title: Re: Send event in script
Post by: e.Ko on April 04, 2011, 06:37:22 AM
If behavior is a public variable, did you drag a scene object onto it?
If you're finding the object at runtime, check that it's not null...
Title: Re: Send event in script
Post by: BeNamed on April 04, 2011, 06:50:40 AM
I used Add Script to my player and I select my script.
I want to add my script to a specific state so I can do my calculations in it. And when a key press happens (I check this in my script) he has to go to the next event. (or to the event I want)

I am using Prefabs!
Title: Re: Send event in script
Post by: Alex Chouls on April 04, 2011, 01:27:32 PM
The script should initialize behavior to reference a PlayMakerFsm on a scene object (e.g., an instantiated prefab)... Debug log out the value of behavior to check what's happening...

Title: Re: Send event in script
Post by: BeNamed on April 05, 2011, 03:17:18 AM
I found the problem. The behavior was never initialized. So I added this:

Code: [Select]
behavior = (PlayMakerFSM)gameObject.GetComponent(typeof(PlayMakerFSM));
Now the behavior is filled with the PlayMakerFSM of the object I'm in.
It works now!

Thanks
Title: Re: Send event in script
Post by: MaDDoX on April 05, 2011, 05:29:00 PM
Prefabs are always a pain in the b*** like this.. I rather simply having a disabled instance (we call it "prototype") and clone/instance from it instead of from a prefab. This way I don't have to do large amounts of housekeeping just to initialize references.

I remember reading in the Unity forums that this was an optimal approach for iOS-development since it was more performatic, but my lead coder doesn't like it much 'coz if you change a variable name in the script Unity loses reference to any inspector-created links. Personally I don't care, I just name my variables properly from the start *ahem* :) Does anyone know of any "serious" downside to that approach?