playMaker

Author Topic: Moving Platform Custom Script Error [SOLVED]  (Read 1833 times)

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Moving Platform Custom Script Error [SOLVED]
« on: July 03, 2016, 01:34:34 PM »
Hi

I have been working on a custom script for a moving platform and i had it working in Unity 4.6 but now i have updated to Unity 5 i get one error on line 121. Any ideas why?

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2015. All rights reserved.
// Action made by NickCoxMedia
// Moving Platform Transition Ping Pong
// __ECO__ __PLAYMAKER__  __ACTION__

using UnityEngine;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{

[ActionCategory(ActionCategory.Transform)]
[Tooltip("This will transform a object from one position to another with the option to pause. A ping pong like motion.")]
public class MovingPlatform : FsmStateAction
{
[RequiredField]
[Tooltip("Game Object to move")]
public FsmOwnerDefault gameObject; //owner

[RequiredField]
[Tooltip("Choose a starting point")]
public FsmOwnerDefault StartingPoint;

[RequiredField]
[Tooltip("Choose a destination point")]
public FsmOwnerDefault DestinationPoint;

[Tooltip("How fast the platform moves. Use decimal places to slow down further e.g. 0.02")]
public FsmFloat Speed;

[Tooltip("How long to delay before moving")]
public FsmFloat delayBeforeMoving;

[Tooltip("shows when the platform returns")]
private FsmBool Switch = false;

private FsmFloat pauseTime;
private bool arrivedAtOurDestination = false;

public PlayMakerActionsUtils.EveryFrameUpdateSelector updateType;


public override void Reset()
{
gameObject = null;
DestinationPoint = null;
StartingPoint = null;
Switch = false;
delayBeforeMoving = 3f;
pauseTime = 0f;
Speed = 1f;
updateType = PlayMakerActionsUtils.EveryFrameUpdateSelector.OnUpdate;
}

public override void Awake()
{
if (updateType == PlayMakerActionsUtils.EveryFrameUpdateSelector.OnFixedUpdate)
{
Fsm.HandleFixedUpdate = true;
}
}

public override void OnUpdate()
{
if (updateType == PlayMakerActionsUtils.EveryFrameUpdateSelector.OnUpdate)
{
DoMovingPlatform();
}
}

public override void OnLateUpdate()
{
if (updateType == PlayMakerActionsUtils.EveryFrameUpdateSelector.OnLateUpdate)
{
DoMovingPlatform();
}
}

public override void OnFixedUpdate()
{
if (updateType == PlayMakerActionsUtils.EveryFrameUpdateSelector.OnFixedUpdate)
{
DoMovingPlatform();
}
}


void DoMovingPlatform()
{

var owner = Fsm.GetOwnerDefaultTarget(gameObject); //get owner
var destination = Fsm.GetOwnerDefaultTarget(DestinationPoint); //get destination
var origin = Fsm.GetOwnerDefaultTarget(StartingPoint); //get StartingPoint

if (owner != null && destination != null && origin != null)
{

// If it's at the destination spot, it sets Switch to true.
if((owner.transform.position == destination.transform.position) && !arrivedAtOurDestination)

{
Switch = true;
pauseTime = Time.time + delayBeforeMoving.Value;
arrivedAtOurDestination = true;
}

// If it's at the origin spot, it sets Switch to false.
if((owner.transform.position == origin.transform.position) && !arrivedAtOurDestination)
{
Switch = false;
pauseTime = Time.time + delayBeforeMoving.Value;
arrivedAtOurDestination = true;
}

// If Switch becomes true, it tells the platform to move to its Origin.
if(Switch.Value && (Time.time > pauseTime.Value))
{
owner.transform.position = Vector3.MoveTowards(owner.transform.position, origin.transform.position, Time.deltaTime * Speed.Value); //use delta time so time scale works
arrivedAtOurDestination = false;
}
else if (Time.time > pauseTime.Value)
{
// If Switch is false, it tells the platform to move to the destination.
owner.transform.position = Vector3.MoveTowards(owner.transform.position, destination.transform.position, Time.deltaTime * Speed.Value); //use delta time so time scale works
arrivedAtOurDestination = false;
}
}

}
    }
}


errors in unity
NullReferenceException: Object reference not set to an instance of an object
HutongGames.PlayMaker.Actions.MovingPlatform.DoMovingPlatform () (at Assets/PlayMaker Custom Actions/Transform/MovingPlatform.cs:121)
HutongGames.PlayMaker.Actions.MovingPlatform.OnFixedUpdate () (at Assets/PlayMaker Custom Actions/Transform/MovingPlatform.cs:83)
HutongGames.PlayMaker.FsmState.OnFixedUpdate () (at c:/Users/Alex/Documents/Unity/Playmaker/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:237)
HutongGames.PlayMaker.Fsm.FixedUpdateState (HutongGames.PlayMaker.FsmState state) (at c:/Users/Alex/Documents/Unity/Playmaker/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2496)
HutongGames.PlayMaker.Fsm.FixedUpdate () (at c:/Users/Alex/Documents/Unity/Playmaker/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:1831)
PlayMakerFixedUpdate.FixedUpdate () (at c:/Users/Alex/Documents/Unity/Playmaker/Projects/Playmaker.source.unity/Assets/PlayMaker/PlayMakerFixedUpdate.cs:17)
« Last Edit: July 04, 2016, 03:03:05 PM by coxy17 »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Moving Platform Custom Script Error
« Reply #1 on: July 04, 2016, 03:27:50 AM »
Hi,

You are using a FsmFloat as a private value, this is wrong.

 only public variables should be using FsmXXX types. So for pauseTime, use a regular float, else you need to check for null as the FsmFloat isn't internally instanciated because it's private.

Bye,

 Jean

coxy17

  • Beta Group
  • Sr. Member
  • *
  • Posts: 355
Re: Moving Platform Custom Script Error
« Reply #2 on: July 04, 2016, 03:02:44 PM »
Hi Jean

Thanks for this, it worked great.

Nick