1

I'm doing the hummingbird course and when I try to do the heuristic test I get the error pictured below: Object reference not set to an instance of an object in the OnEpisodeBegin function on line 76 which reads flowerArea.ResetFlowers(); (see HummingbirdAgent.cs). This makes no sense to me because flowerArea is declared above on line 34. Here's a picture of the error

Here's the OnEpisodeBegin() function

public override void OnEpisodeBegin()
{
    if (trainingMode)
    {
        //Only reset flowers in training when there is one agent per area
        flowerArea.ResetFlowers();
    }

    //Reset nectar obtained
    NectarObtained = 0f;

    // Zero out velocities so that movement stops before a new episode begins
    rigidbody.velocity = Vector3.zero;
    rigidbody.angularVelocity = Vector3.zero;

    //Default to spawning in front of a flower
    bool inFrontOfFlower = true;
    if (trainingMode)
    {
        //Spawn in front of flower 50% of the time during training
        inFrontOfFlower = UnityEngine.Random.value > .5f;
    }

    // Move the agent to a new random position
    MoveToSafeRandomPosition(inFrontOfFlower);

    // Recalculate the nearest flower now that the agent has moved
    UpdateNearestFlower();
}

and here's the declaration on line 34:

private FlowerArea flowerArea;
ndunn92
  • 11
  • 1
  • To call a method on a variable (like `.ResetFlowers()`) it's not enough to just declare the variable. The variable must also be set to a non-null value. When you declare a variable without assigning to it somehow, its value will be null by default. – Mike Clark Jul 17 '22 at 04:40
  • looks like you are not assigned a value to the object – shamnad sherief Jul 17 '22 at 05:02
  • Sorry, there's also this line in the Initialize function: FlowerArea flowerArea = GetComponentInParent(); – ndunn92 Jul 17 '22 at 07:21

0 Answers0