I'm trying to make a conga line of objects. Am I using arrays wrong?

I’m trying to make a game where objects can be collected (via a trigger) and will join a line of identical objects to make a conga line.

Obviously each object should be following the one directly in front of it, so my idea was to use an array so it could identify which object within that array should be followed, based on how many are already following the player, which is why I’m using a static variable so they can all keep track of how many are currently following.

However, what I have now isn’t working. It says ‘Object reference not set to an instance of an object’. Am I using arrays wrong?

using System.Collections;
using UnityEngine;

public class FollowerScript : MonoBehaviour
{
    bool isActive = true;
    static int followerAmount = 0;
    public GameObject playerRabbit;
    GameObject followTarget;
    float DistanceFromTarget;
    Animator animator;

    void Start()
    {
        animator.GetComponent<Animator>();
    }

    void FixedUpdate()
    {
        //Rabbit will only follow if it has been activated.
        if (isActive)
        {
            FollowTheTarget();
        }
    }
    
    //When the player moves close to the follower it will activate.
    private void OnTriggerEnter(Collider other)
    {
        //Array contains all the followers and the player character.
        GameObject[] Rabbits = GameObject.FindGameObjectsWithTag("Rabbit");    
        playerRabbit = Rabbits[0];
        // If the collided object is the player and the follower has not already been activated.
        if (other == playerRabbit && !isActive)
        {
            //Default follower amount is zero, the player.
            followTarget = Rabbits[followerAmount];
            isActive = true;
            followerAmount++;
        }
    }

    void FollowTheTarget()
    {
        //Makes the followers stop a certain distance from the target.
        DistanceFromTarget = Vector3.Distance(transform.position, followTarget.transform.position);
        if (DistanceFromTarget > 2)
        {
            //Moves the follower towards the target + animations.
            transform.position = Vector3.MoveTowards(transform.position, followTarget.transform.position, 0.3f);
            animator.SetBool("IsRunning", true);
            transform.LookAt(followTarget.transform);
        }
        else
        {
            animator.SetBool("IsRunning", false);
        }
    }
}

Hello.

First, for all Null reference error, we post this meesage :


Good day. This is a “standard response”
For this kind of problems, you need to learn to find your problem by your own. As you can imagine, we can not try to read all scripts people posts, understand the logic and process, what all variables means, when or how you use them, and find where is the “problem”. You are the only one who can do it…
You need to debug the code while running, and check the states of all variables at the moment you see the problem, and I’m sure you will detect what is not how it was suposed to be.
If don’t know how, look for some tutorials on how to debug code while running.
Bye & good Luck!


Learn about Null reference, is the most common error. It means some variable is null when code tries to read it.

If you dont say at what lineyou have the rror, is 100% impossible to guide you, but look, i say guide, not solve the problem. The problem can be obnly solved by you, we can not test the code, only you can.

Learn to debug and detect what variable is still null and then investigate why.

good luck!