-2

so i'm learning to code in unity with this tutorial, but in time 3:10:30 when he makes FloatingTextManager, it gives me this error and i don't know how to fix it. I know, what the error means but i don't know how or where do i fix it :/

NullReferenceException: Object reference not set to an instance of an object
FloatingTextManager.GetFloatingText () (at Assets/Scripts/FloatingTextManager.cs:42)
FloatingTextManager.Show (System.String msg, System.Int32 fontSize, UnityEngine.Color color, UnityEngine.Vector3 position, UnityEngine.Vector3 motion, System.Single duration) (at Assets/Scripts/FloatingTextManager.cs:21)
GameManager.ShowText (System.String msg, System.Int32 fontSize, UnityEngine.Color color, UnityEngine.Vector3 position, UnityEngine.Vector3 motion, System.Single duration) (at Assets/Scripts/GameManager.cs:42)
Chest.OnCollect () (at Assets/Scripts/Chest.cs:16)
Collectable.OnCollide (UnityEngine.Collider2D coll) (at Assets/Scripts/Collectable.cs:13)
Colliadable.Update () (at Assets/Scripts/Colliadable.cs:25)

Here is FloatingTextManager.cs

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

public class FloatingTextManager : MonoBehaviour
{
    public GameObject textContainer;
    public GameObject textPrefab;

    private List<FloatingText> floatingTexts = new List<FloatingText>();

    private void Update()
    {
        foreach (FloatingText txt in floatingTexts)
            txt.UpdateFloatingText();
    }

    public void Show(string msg, int fontSize, Color color, Vector3 position, Vector3 motion, float duration)
    {
        FloatingText floatingText = GetFloatingText();

        floatingText.txt.text = msg;
        floatingText.txt.fontSize = fontSize;
        floatingText.txt.color = color;

        floatingText.go.transform.position = Camera.main.WorldToScreenPoint(position);
        floatingText.motion = motion;
        floatingText.duration = duration;

        floatingText.Show();
    }

    private FloatingText GetFloatingText()
    {
        FloatingText txt = floatingTexts.Find(t => !t.active);

        if(txt == null)
        {
            txt = new FloatingText();
            txt.go = Instantiate(textPrefab).GetComponent<GameManager>();
            txt.go.transform.SetParent(textContainer.transform);
            txt.txt = txt.go.GetComponent<Text>();

            floatingTexts.Add(txt);
        }

        return txt;
    }
}

And here is FloatingText.cs

public class FloatingText
{
    public bool active;
    public GameManager go;
    public Text txt;
    public Vector3 motion;
    public float duration;
    public float lastShown;
 
    public void Show()
    {
        active = true;
        lastShown = Time.time;
        go.GetComponent<GameObject>().SetActive(active);
    }
 
    public void Hide()
    {
        active = false;
        go.GetComponent<GameObject>().SetActive(active);
    }
 
    public void UpdateFloatingText()
    {
        if (!active)
            return;
 
        if (Time.time - lastShown > duration)
            Hide();
 
        go.transform.position += motion * Time.deltaTime;
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Where is the `FloatingText` class? And what line of code throws the exception? – JohnG Oct 09 '21 at 15:48
  • @JohnG https://pastebin.com/bWDyezji this is FloatingText.cs, and i think that this -> at Assets/Scripts/FloatingTextManager.cs:42 <- answers your question – HelloItsMeAdm Oct 09 '21 at 15:59
  • In general I don't know what kind of tutorial this is supposed to be but `GetComponent()` is simply **wrong**. A `GameObject` is no component -> you should get an error telling you that this type can't be used for `GetCompoment`. It would simply be `go.gameObject.SetActive` ... – derHugo Oct 09 '21 at 18:17
  • And does your `textPrefab` have a `GameManager` attached at all? Otherwise `GetComponent()` will always return `null` ... – derHugo Oct 09 '21 at 18:18
  • i don't know ho to do that...im new in unity and c# – HelloItsMeAdm Oct 09 '21 at 19:22

1 Answers1

-1

The line txt.go = Instantiate(textPrefab).GetComponent<GameManager>() doesn't make sense; the GetComponent returns a GameManager but txt.go is a GameObject, and the GetComponent isn't included in the video you linked. This shouldn't compile though so I'm assuming this isn't the cause of the error you mentioned.

Assuming the line numbers are still correct, line 42 is txt.go.transform.SetParent(textContainer.transform);. Since txt has just been instantiated and txt.go has presumably been set to something in the previous line, textContainer is most likely the cause of the NullReferenceException, by being null.

You can see at 3:04:22 in the tutorial you linked that the textContainer field has to be assigned in the editor. This is most likely the step you missed, causing it to be null.

jdewi
  • 26
  • 3
  • So i removed `.GetComponent()` in the first part and i linked FloatingTextManager like [that](https://i.imgur.com/6fToGsX.png), but now its giving me error `Assets\Scripts\FloatingTextManager.cs(41,22): error CS0029: Cannot implicitly convert type 'UnityEngine.GameObject' to 'GameManager'`, what did i did wrong? – HelloItsMeAdm Oct 09 '21 at 16:06
  • @HelloItsMeAdm It seems like the field `txt.go` is of type `GameManager` rather than `GameObject`. You should edit the `FloatingText` script and replace `GameManager go` with `GameObject go`. You can see the same mistake being made in the video at 2:42:55 before being quickly corrected. – jdewi Oct 09 '21 at 16:13
  • Thanks, this was propably by wrong autotabcompletion. But when i collide with the chest another 2 errors apper, do you know what could be wrong? https://pastebin.com/yz8yCjGv – HelloItsMeAdm Oct 09 '21 at 16:16
  • @HelloItsMeAdm Sorry I have to go to sleep now. I've just noticed in the screenshot you linked just before, you seem to have the FloatingTextManager GameObject referenced for the `Text Prefab`, instead of the FloatingText prefab. (Edit: Accidentally posted) That first error suggests to me that `txt.go` is a prefab instead of a GameObject in the scene. Did you perhaps write `txt.go = textPrefab` instead of `txt.go = Instantiate(textPrefab)`? If these don't solve your issue I'd suggesting going back through the video and making sure your project matches. – jdewi Oct 09 '21 at 16:31
  • I messed up with some things and half part started working. But now im getting another error and problem. The text is not animated, its big etc. https://imgur.com/AgekihR https://pastebin.com/TRDdyh2D – HelloItsMeAdm Oct 09 '21 at 16:32
  • I've tried remaking the whole category, but still nothing. I don't know what to do now :/ – HelloItsMeAdm Oct 09 '21 at 16:57
  • Well what of the field was supposed to be a `GameManager` and just the name is stupid? ;) You might break a lot of things by simply suggesting to change some fields type ... – derHugo Oct 09 '21 at 18:13
  • yeah but where do i have to change it? i checked it 2 times :/ – HelloItsMeAdm Oct 09 '21 at 18:20
  • @derHugo in the tutorial linked in the question, the field is a GameObject, and the GetComponent isn't there. – jdewi Oct 09 '21 at 23:27
  • @jdewi fair enough .. tbh I don't even bother to click on a link if OP says the video is at least 3:10:30 :'D – derHugo Oct 10 '21 at 06:19