playMaker

Author Topic: edited "Addforce2d" to have a space for local and global but it shows null  (Read 309 times)

arminiuspp

  • Playmaker Newbie
  • *
  • Posts: 37
hello, so I edit the script for addforce2d, rigidbody 2d is good and all too but it keeps shooting this error for me when I create the object.

NullReferenceException: Object reference not set to an instance of an object
HutongGames.PlayMaker.Actions.AddForce2d.DoAddForce () (at Assets/PlayMaker Custom Actions/ChatGPT/AddForce2d2.cs:100)
HutongGames.PlayMaker.Actions.AddForce2d.OnEnter () (at Assets/PlayMaker Custom Actions/ChatGPT/AddForce2d2.cs:65)
HutongGames.PlayMaker.FsmState.ActivateActions (System.Int32 startIndex) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:222)
HutongGames.PlayMaker.FsmState.OnEnter () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:192)
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3073)
HutongGames.PlayMaker.Fsm.SwitchState (HutongGames.PlayMaker.FsmState toState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3012)
HutongGames.PlayMaker.Fsm.UpdateStateChanges () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2945)
HutongGames.PlayMaker.Fsm.Start () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2169)
PlayMakerFSM.Start () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/PlayMakerFSM.cs:595)


----

here is the edited version of it
// (c) Copyright HutongGames, LLC 2010-2016. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
   [ActionCategory(ActionCategory.Physics2D)]
   [Tooltip("Adds a 2D force to a Game Object. Use Vector2 variable and/or Float variables for each axis.")]
   [SeeAlso("To add a force in local space use {{Add Relative Force 2d}}")]
   public class AddForce2d : ComponentAction<Rigidbody2D>
   {
      [RequiredField]
      [CheckForComponent(typeof(Rigidbody2D))]
      [Tooltip("The GameObject to apply the force to.")]
      public FsmOwnerDefault gameObject;

      [Tooltip("Option for applying the force")]
      public ForceMode2D forceMode;

      [Tooltip("Optionally apply the force at a position on the object. This will also add some torque. The position is often returned from MousePick or GetCollision2dInfo actions.")]
      public FsmVector2 atPosition;

      [Tooltip("A Vector2 force to add. Optionally override any axis with the X, Y parameters.")]
      public FsmVector2 vector;

      [Tooltip("Force along the X axis. To leave unchanged, set to 'None'.")]
      public FsmFloat x;

      [Tooltip("Force along the Y axis. To leave unchanged, set to 'None'.")]
      public FsmFloat y;

      [Tooltip("A Vector3 force to add. z is ignored")]
      public FsmVector3 vector3;

      [Tooltip("Repeat every frame while the state is active.")]
      public bool everyFrame;

      [Tooltip("Add the force in local or global space.")]
      public Space space;

      public override void Reset()
      {
         gameObject = null;
         atPosition = new FsmVector2 { UseVariable = true };
         forceMode = ForceMode2D.Force;
         vector = null;
         vector3 = new FsmVector3 { UseVariable = true };

         // default axis to variable dropdown with None selected.
         x = new FsmFloat { UseVariable = true };
         y = new FsmFloat { UseVariable = true };

         everyFrame = false;
         space = Space.Self;
      }


      public override void OnPreprocess()
      {
         Fsm.HandleFixedUpdate = true;
      }

      public override void OnEnter()
      {
         DoAddForce();

         if (!everyFrame)
         {
            Finish();
         }
      }

      public override void OnFixedUpdate()
      {
         DoAddForce();
      }

      void DoAddForce()
      {
         var go = Fsm.GetOwnerDefaultTarget(gameObject);
         if (!UpdateCache(go))
         {
            return;
         }

         var force = vector.IsNone ? new Vector2(x.Value, y.Value) : vector.Value;

         if (!vector3.IsNone)
         {
            force.x = vector3.Value.x;
            force.y = vector3.Value.y;
         }

         // override any axis
         if (!x.IsNone) force.x = x.Value;
         if (!y.IsNone) force.y = y.Value;

         if (space == Space.Self)
         {
            cachedComponent.AddForce(cachedTransform.TransformDirection(force), forceMode);
         }
         else
         {
            cachedComponent.AddForce(force, forceMode);
         }

         if (!atPosition.IsNone)
         {
            cachedComponent.AddForceAtPosition(force, atPosition.Value, forceMode);
         }
      }
   }
}


with all this in mind, it shoots the error in playmode but it doesn't pause it or anything, might crash the mobile device though. does anyone know whats happening here (Just edited this with chatgpt I don't know anything about coding)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
The error show that its in line 100
what on line 100

null reference means that a variable (for example game object) is null (empty)

it might be this line.
try replacing this line :
Code: [Select]
    cachedComponent.AddForce(cachedTransform.TransformDirection(force), forceMode);
with this :
Code: [Select]
cachedComponent.AddForce(cachedComponent.transform.TransformDirection(force), forceMode);

arminiuspp

  • Playmaker Newbie
  • *
  • Posts: 37
Thanks! it works!