Custom action isn't functioning in build.

Captain

New member
I picked up behavior designer the other day for my work project. and to see if it'll work well, I did a prototype today. everything works really well and I'm very happy with it, in the editor. but when I go to run the built version the Agent doesn't behave correctly and this error is spammed constantly in the log.

(Filename: C:/Users/me/Desktop/Adventures in Evolution/Assets/Scripts/Creature.cs Line: 96)

NullReferenceException: Object reference not set to an instance of an object
at Creature.Consume (UnityEngine.GameObject food) [0x00023] in C:\Users\me\Desktop\Adventures in Evolution\Assets\Scripts\Creature.cs:96
at BehaviorDesigner.Runtime.Tasks.Eat.OnUpdate () [0x00001] in C:\Users\me\Desktop\Adventures in Evolution\Assets\Scripts\Tasks\Eat.cs:23
at BehaviorDesigner.Runtime.BehaviorManager.RunTask (BehaviorDesigner.Runtime.BehaviorManager+BehaviorTree behaviorTree, System.Int32 taskIndex, System.Int32 stackIndex, BehaviorDesigner.Runtime.Tasks.TaskStatus previousStatus) [0x00194] in <cf5dc968a0264c9f89a51575f59f24d0>:0
at BehaviorDesigner.Runtime.BehaviorManager.Tick (BehaviorDesigner.Runtime.BehaviorManager+BehaviorTree behaviorTree) [0x001c2] in <cf5dc968a0264c9f89a51575f59f24d0>:0
at BehaviorDesigner.Runtime.BehaviorManager.Tick () [0x00014] in <cf5dc968a0264c9f89a51575f59f24d0>:0
at BehaviorDesigner.Runtime.BehaviorManager.Update () [0x00000] in <cf5dc968a0264c9f89a51575f59f24d0>:0

C#:
    public void Consume(GameObject food)
    {
        if (food != null)
        {
            var newEnergy = (SharedFloat)behaviorTree.GetVariable("Energy");
            newEnergy.Value += food.GetComponent<Plant>().energy; //Line 96
            WorldGenerator.worldGen.currentFood.Remove(food);
            Destroy(food);
        }
    }

C#:
        public override TaskStatus OnUpdate()
        {
            self.Value.Consume(target.Value); // Line 23
            return TaskStatus.Success;
        }

I really am not sure why this is happening, I tried a few different things to fix it, any help would be greatly appreciated.
 

Justin

Administrator
Staff member
What is line 96 of Creature.cs? My guess is that one of the component references is null.
 

Captain

New member
The component reference is null. So I guess my question then becomes how do I correctly get a component from a sharedVariable.

C#:
using UnityEngine;

namespace BehaviorDesigner.Runtime.Tasks
{
    [TaskDescription("Consumes the target.")]
    [TaskCategory("Creature")]
    [TaskIcon("Assets/Behavior Designer Movement/Editor/Icons/{SkinColor}SeekIcon.png")]

    public class Eat : Action
    {
        public SharedCreature self;

        //[Tooltip("The GameObject that the agent should consume")]
        public SharedGameObject target;

        public override TaskStatus OnUpdate()
        {
            self.Value.Consume(target.Value.gameObject.GetComponent<Plant>()); 
            return TaskStatus.Success;
        }
    }
}

but this line "self.Value.Consume(target.Value.gameObject.GetComponent<Plant>());" always returns null and I've looked in the documentation. As far as my knowledge takes me, target's value is a gameobject with a Plant component so this line shouldn't give a null reference to the Consume method.
 

Justin

Administrator
Staff member
It sounds like the target variable is pointing to a null value. Make sure your correctly assign the value before using it.
 

Captain

New member
Yeah that was it. I was just confused because the eat action I created was pointing to a valid shared target variable, but it was placed after a seek action, and I guess the seek action must have been nullifying it right before the eat action would use it. Thanks a ton man, really appreciate the help.
 
Top