playMaker

Author Topic: Null Reference Exception: Can't pass GameObject var to C# script [SOLVED]  (Read 1006 times)

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 256
I'm having a coin magnet script on each coin and when the Magnet Collider gets into the trigger zone of the coin I use a custom action to call a method on the script that basically sets a boolean to true so the coin can get attracted by the player. I also pass the Player Game Object to the script so it knows what transform.position it has to copy and move towards to.

The problem is: it never passes the Player Game Object and I can't find out what's the reason.

Here is the custom action I wrote:

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory("Collectible Magnet")]
    [Tooltip("Calls the Method 'ActivateMagnetForce' in the CollectibleMagnet script.")]

    public class ActivateMagnetForce : FsmStateAction
    {
        [Tooltip("The Game Object the CollectibleMagnet script is attached to. Example: a Coin.")]
        public FsmOwnerDefault collectible;

        [RequiredField]
        [CheckForComponent(typeof(CollectibleMagnet))]
        [Tooltip("The Game Object the collectible should be attracted to. Normally the Player.")]
        public FsmGameObject targetObject;

        public override void Reset()
        {
            collectible = new FsmOwnerDefault();
            targetObject = new FsmGameObject();
        }

        public override void OnEnter()
        {
            Fsm.GetOwnerDefaultTarget(collectible).GetComponent<CollectibleMagnet>().ActivateMagnetForce(targetObject.Value);

            Finish();
        }
    }
}

Here is the counterpart of the Magnet script:
Code: [Select]
    //activated with the Playmaker Action "Activate Magnet Force"
    public void ActivateMagnetForce(GameObject player)
    {
        Debug.Log("PM Action works!");
        _player = player;
        _targetTransform = player.transform;
        _isCoinMoving = true;
    }

And this is the error I get:


I also tried instead of FsmOwnerDefault to use FsmGameObject instead. Same problem though.

What am I doing wrong? Any help is appreciated.
« Last Edit: December 08, 2022, 06:26:34 PM by Christoph »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7618
    • jinxtergames
Hi.
Try
Code: [Select]
targetObject.Value.GetComponent<CollectibleMagnet>().ActivateMagnetForce(targetObject.Value);

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 256
Oops I actually forgot to change the collectible back which is the one that has the CollectibleMagnet script attached.

The correct code would be:
Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory("Collectible Magnet")]
    [Tooltip("Calls the Method 'ActivateMagnetForce' in the CollectibleMagnet script.")]

    public class ActivateMagnetForce : FsmStateAction
    {
        [CheckForComponent(typeof(CollectibleMagnet))]
        [Tooltip("The Game Object the CollectibleMagnet script is attached to. Example: a Coin.")]
        public FsmOwnerDefault collectible;
       
        [Tooltip("The Game Object the collectible should be attracted to. Normally the Player.")]
        public FsmGameObject targetObject;

        public override void Reset()
        {
            collectible = new FsmOwnerDefault();
            targetObject = new FsmGameObject();
        }

        public override void OnEnter()
        {
            //Fsm.GetOwnerDefaultTarget(collectible).GetComponent<CollectibleMagnet>().ActivateMagnetForce(targetObject.Value);
            collectible.GetComponent<CollectibleMagnet>().ActivateMagnetForce(targetObject.Value);
            Finish();
        }
    }
}

But since the collectible is still a FsmOwnerDefault object, that code is giving error. Should I just change it to a normal FsmGameObject?

Edit:
Tried with FsmGameObject and now the following line of code works:
Code: [Select]
collectible.Value.GetComponent<CollectibleMagnet>().ActivateMagnetForce(targetObject.Value);
BUT... at runtime, it still shows NullReferenceException: Object reference not set to an instance of an object

So weird cause the Object is obviously there.
« Last Edit: December 08, 2022, 03:03:35 PM by Christoph »

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 256
Ok this actually works. My mistake, I am moving the child (the coin) and it was looking for the parent object (with the script attached). Error message was misleading since it wasn't about the Game Object passing but about the script component not being found.

Marked as resolved!