- Home /
error CS0713 UnityEngine.MonoBehaviour'. Static classes must derive from object
I've add the static code to the MyClock script and it giving me this error. I need to gain access to MyClock script from PlayerPreference script . The only way I can access my clock is by making the script static and I did that . But I getting error. Here the my clock script and PlayerPreference Script:
using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections;
public static class MyClock : MonoBehaviour { public string time; public int Minutes = 0; public int Seconds = 0;
public Text m_text;
public float m_leftTime;
public void Start()
{
m_text = GetComponent<Text>();
m_leftTime = GetInitialTime();
}
public void Update()
{
if (m_leftTime > 0f)
{
// Update countdown clock
m_leftTime -= Time.deltaTime;
Minutes = GetLeftMinutes();
Seconds = GetLeftSeconds();
// Show current clock
if (m_leftTime > 0f)
{
m_text.text = "Time : " + Minutes + ":" + Seconds.ToString("00");
}
else
{
// The countdown clock has finished
m_text.text = "Time : 0:00";
}
}
}
private float GetInitialTime()
{
return Minutes * 60f + Seconds;
}
private int GetLeftMinutes()
{
return Mathf.FloorToInt(m_leftTime / 60f);
}
private int GetLeftSeconds()
{
return Mathf.FloorToInt(m_leftTime % 60f);
}
}
using UnityEngine.SceneManagement; using UnityEngine; using UnityEngine.UI; using System.Collections; using myclock;
public class PlayerPreference : MonoBehaviour { private ScoreManager scoreManagerScript ; public GameObject player; private myclock MyClockScript;
void Awake ()
{
GameObject Player = GameObject.FindWithTag("Player");
scoreManagerScript = Player.GetComponent<ScoreManager>();
MyClockScript = GetComponent<myclock>();
}
void Update(){
if ((myclock!= NULL) && (scoreManager != NULL) &&
(myclock.m_leftTime >= scoreManager.score))
{
SceneManager.LoadScene("time");
}
}
}
$$anonymous$$aking it static is not the way to do that. Static classes can't be instantiated, and can only contain static members..., so they're never going to work as $$anonymous$$onoBehaviours.
If there's only one clock you could make the class into singleton and access it that way.
Another way to go about it is to get a reference to the clock (either in code or via the inspector) and access it through that.
Give me an example in code singleton script my script . The myclock script
Google Unity singleton. There's code out there for this, but I'd suggest using GetComponent ins$$anonymous$$d.
" The only way I can access my clock is by making the script static and I did that " No. That's what GetComponent is for. You never need static classes and I strongly recommend you avoid them - they will cause you trouble further down the line.
https://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent
Your answer
Follow this Question
Related Questions
Can anyone provide some help with that error ? 1 Answer
No Monobehaviour scripts in files 1 Answer
I can't add my script to a boss 2 Answers
Problem with attaching scripts to objects 1 Answer
Missing definitions and script error 1 Answer