[NullReferenceException] Code works the way I want but still returns error

Hi, I’m trying to make an in-game log that actually runs the way I want it to but it returns an error in the console:

NullReferenceException: Object reference not set to an instance of an object
log.newActivity (System.String activity) (at Assets/scripts/log.cs:22)
log.FixedUpdate () (at Assets/scripts/log.cs:32)

Here is the code for the script I’m using:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

// http://answers.unity3d.com/questions/508268/i-want-to-create-an-in-game-log-which-will-print-a.html

public class log : MonoBehaviour {

    private int maxLines = 500;
    private Queue<string> queue = new Queue<string>();

    public Text logText;

    // adds new line to in game log at the bottom and retains a certain number of lines, defined by int maxLines
    public void newActivity(string activity) {
        if (queue.Count >= maxLines) {
            queue.Dequeue ();
        }
        queue.Enqueue(activity);

        logText.text = "";
        foreach (string st in queue) {
            logText.text = logText.text + "\n" + st;
        }
        Canvas.ForceUpdateCanvases ();
    }

    // debug method to test log functionality
    public void FixedUpdate() {
        if (Input.GetKeyDown ("f")) {
            newActivity ("An activity was added to the log.");
        }
    }
}

Attached are screenshots of the inspector showing that I have assigned a Text to the Log Text field in the inspector and of what happens when I run the script in game.

Thanks in advance for any help. If any more information is needed, I will provide it if I can.

Do you have multiple log scripts in your scene? That’s what it looks like to me.

1 Like

I feel so stupid… That was exactly right. Thank you so much.