4

I am new to C# and Unity and am having trouble finding a clear answer to my problem.

I am trying to create a simple TextMeshProUGUI text log buffer in a panel. The buffer itself works fine until I try to access it from another class - I believe because I am not creating the reference to the panel correctly.

Here is my code for the TextMeshProUGUI object collector:

using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class TextLogControl : MonoBehaviour
{
    public TextMeshProUGUI textPrefab;  // Unity prefab 

    public  List<TextMeshProUGUI> textItems = new List<TextMeshProUGUI>();

    [SerializeField]
    public int maxItems = 100;


    public void LogText(string newTextString, Color newColor)
    {

    Instantiate(textPrefab, transform);

        textPrefab.text = newTextString;

        if (textItems.Count >= maxItems)
        {
            textItems.RemoveAt(0); // I should probably be destroying something, but that's another question
        }

        textPrefab.gameObject.SetActive(true);

        textItems.Add(textPrefab);
    }

    // The above function works correctly if I write a test function within this same class

}

Here is the code for the class that is trying to access the LogText() function:

using System;
using UnityEngine;

public class World : MonoBehaviour
{
    Color defaultColor = Color.black;

    public TextLogControl textLog; 


    public void Init()
    {
        // I need to create a reference here somewhere, but nothing I am trying is working

        textLog.LogText("Welcome - you made it!", defaultColor);

    }
}

I am putting the TextLogControl script on the Unity GameObject that is holding the TMP objects, and that works on its own.

I thought that I was creating a reference to the holder GameObject by dragging it onto my World object in Unity as below, but I am still getting an NRE when I call World.Init(), which means that I'm doing something wrong, but I cannot figure out what.

I thought this would create the reference that is not being created

I thought this would create the reference that is not being created

Edit: The error I'm receiving is

NullReferenceException: Object reference not set to an instance of an object

When trying to run World.Init() - specifically, textLog is null, even though I have got it dragged onto the appropriate spot in Unity (I believe).

12
  • instantiate at transform.position not transform Commented Jan 29, 2020 at 11:46
  • also post that the error that you are facing Commented Jan 29, 2020 at 11:47
  • 1
    Thanks - sorry, first question! I've edited to include the error message. Commented Jan 29, 2020 at 11:52
  • 1
    Apparently it's this docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html Commented Feb 2, 2020 at 21:19
  • 1
    It's possible that, due to the order in which the components are created in Play mode, your textLog hasn't yet been assigned by the engine when .Init() is called. Remember that, when you run the game, everything has to be created and initialized, and that happens in a specific sequence. Also, consider using the default Start() method for your initialization rather than rolling your own. Commented Feb 2, 2020 at 21:26

1 Answer 1

2

As this is so long for comment, A null reference means that it is trying to access something that doesn't exist. You either forgot to drag something in the editor, or you are a step ahead and have something un-commented that should still be commented. Your code is using something that isn't there. I recommend you to add this piece of code to your files to check either the error is coming from NullRefrence of class or the else code.

    TextMeshProUGUIs = textPrefab.GetComponent<TextMeshProUGUI>();
    if (TextMeshProUGUIs == null)
    {
        Debug.LogError("No TextMeshProUGUI component found.");  
    }
Sign up to request clarification or add additional context in comments.

1 Comment

What you pasted doesn't compile. The object that is null is textLog - but I have created the reference in Unity and don't understand why it is null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.