I'm not sure if this is a bug, or if I'm doing something terribly wrong. I have a situation where the second time I load a particular scene, I'm receiving a Null Reference Exception on a custom Action, and I've tracked it down to the Owner being Null, which doesn't make a whole lot of sense to me.
The strange thing is the first time I run the scene I have no issues with it, and it's running on multiple entities during the lifetime of the scene. I can pin point it to one particular object, as it's the only one in the scene that has the Action on it when it loads.
Any Ideas?
Here's the script :
public class ProjectileMotion : FsmStateAction
{
public FsmOwnerDefault projectile;
public FsmGameObject startPosObject;
public FsmGameObject endPosObject;
public FsmFloat maxHeight;
public FsmFloat totalTime;
public FsmEvent finishedEvent;
private Vector3 startPos;
private Vector3 endPos;
private Transform projTrans;
private float timer;
public override void Reset ()
{
projectile = null;
finishedEvent = null;
startPosObject = null;
endPosObject = null;
maxHeight = null;
totalTime = null;
}
public override void OnEnter ()
{
// I've tried both methods of retrieval here
//GameObject go = projectile.OwnerOption == OwnerDefaultOption.UseOwner ? Owner : projectile.GameObject.Value;
GameObject go = Fsm.GetOwnerDefaultTarget(projectile);
if(go == null)
{
// This is the line I get the Null Reference on.
Debug.Log("GO return null on : " + Owner.name);
}
projTrans = go.transform;
Debug.Log("Projectile : " + go.name);
startPos = startPosObject.Value.transform.position;
endPos = endPosObject.Value.transform.position;
projTrans.position = startPos;
timer = 0.0f;
}
public override void OnUpdate ()
{
timer += Time.deltaTime;
projTrans.position = GetPointInCurve(startPos, endPos, maxHeight.Value, Mathf.Clamp01(timer/totalTime.Value));
if(timer >= totalTime.Value)
Fsm.Event(finishedEvent);
}
private Vector3 GetPointInCurve(Vector3 start, Vector3 end, float height, float t)
{
return Vector3.Lerp(start, end, t) + Vector3.up * (-1f * Mathf.Pow(t * 2f - 1f, 2f) + 1f) * height;
}
}