Unity выдаёт ошибку
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
public class LevelControl : MonoBehaviour
{
public TextMeshPro text;
private Timer NewTimer;
// Start is called before the first frame update
void Start()
{
NewTimer = new Timer(10);
NewTimer.Go();
}
// Update is called once per frame
void Update()
{
text.text = NewTimer.Time().ToString();
}
}
public class Timer : MonoBehaviour
{
private int timer;
public Timer(int n)
{
timer = n;
}
public void Go()
{
StartCoroutine(TimerStart());
}
private IEnumerator TimerStart()
{
if (timer > 0)
{
timer -= 1;
yield return new WaitForSeconds(1);
StartCoroutine(TimerStart());
}
}
public bool CheckStop()
{
if (timer == 0)
{
return true;
}
return false;
}
public int Time()
{
return timer;
}
}
Я создал вот такой код для таймера в своей игре. В visual studio нет ошибок, однако при запуске появляются предупреждение и ошибка:
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor ()
Timer:.ctor (int) (at Assets/Scripts/LevelControl.cs:226)
LevelControl:Start () (at Assets/Scripts/LevelControl.cs:40)
NullReferenceException
UnityEngine.MonoBehaviour.StartCoroutine (System.Collections.IEnumerator routine) (at <42a5878ce129403083acccf18e43363f>:0)
Timer.Go () (at Assets/Scripts/LevelControl.cs:233)
LevelControl.Start () (at Assets/Scripts/LevelControl.cs:41)
В переменную text я через инспектор прикрепил TextMeshPro