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;
}
}