- Home /
Why isn't GameObject recognised as a class?
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. GameManager.Win () (at Assets/scripts/GameManager.cs:39) GameManager.ScoreUp () (at Assets/scripts/GameManager.cs:33) Ball.OnMouseDown () (at Assets/scripts/Ball.cs:27) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32) (at C:/buildslave/unity/build/Runtime/Export/MouseEvents.cs:187)
That's the error I'm getting!
public class GameManager : MonoBehaviour
{
int score = 0;
public static GameObject winText;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void ScoreUp()
{
score++;
if (score >= 4)
{
Win();
}
}
void Win()
{
winText.SetActive(true);
}
}
You'll have to be more specific, what errors are you getting?
$$anonymous$$issingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. Game$$anonymous$$anager.Win () (at Assets/scripts/Game$$anonymous$$anager.cs:39) Game$$anonymous$$anager.ScoreUp () (at Assets/scripts/Game$$anonymous$$anager.cs:33) Ball.On$$anonymous$$ouseDown () (at Assets/scripts/Ball.cs:27) UnityEngine.Send$$anonymous$$ouseEvents:DoSend$$anonymous$$ouseEvents(Int32) (at C:/buildslave/unity/build/Runtime/Export/$$anonymous$$ouseEvents.cs:187)
That's the error I'm getting! I have created another class named Ball in which I'm trying to access function ScoreUp() of the class! The class named is like: public class Ball : $$anonymous$$onoBehaviour { // Start is called before the first frame update private static Game$$anonymous$$anager game$$anonymous$$anager;
void Start()
{
if (game$$anonymous$$anager == null)
{
game$$anonymous$$anager = FindObjectOfType<Game$$anonymous$$anager>();
}
}
// Update is called once per frame void Update() {
}
public void On$$anonymous$$ouseDown()
{
game$$anonymous$$anager.ScoreUp();
Destroy(this.gameObject);
}
}
It seems like your "winText" object does not exist in the scene. Where do you set the value of this variable? Do you destroy the wintext object at some point? In order for you to call "SetActive" on the object it needs to exist in the scene, which means you should see it in the object hierarchy (it may be light grey to indicate it is inactive, but it should still be there).
Answer by ShadyProductions · Mar 23, 2020 at 04:11 PM
First things first, why is the variable static? static variables persist through scene changes, they last for the entire lifetime of the program. This means that if there is a gameobject referenced and you switch scenes and the gameobject is not persisted through the scene (either through DontDestroyOnLoad), that means your reference is referencing a missing gameobject. Which will result in the error you are receiving.
Either your winText is destroyed at some point, or you are losing the reference. I suggest not making it static, and adding some kind of null check.
Your answer
![](https://koobas.hobune.stream/wayback/20220612225549im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Custom variable declared in JS, that isn't in plugins. 1 Answer
How do I make Vector2 X and Y values into a float? C# 1 Answer
Range of Unitys Variables 2 Answers
Why do Inspector headers with comma-separated variable declarations cause redundant headers? 1 Answer
Possible to use an array value as the name of a variable? 2 Answers