Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by OlgaKogiou · Mar 23, 2020 at 03:28 PM · declaration

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);
     }
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_ek98vnTRplGj8Q · Mar 23, 2020 at 03:39 PM 0
Share

You'll have to be more specific, what errors are you getting?

avatar image OlgaKogiou unity_ek98vnTRplGj8Q · Mar 23, 2020 at 03:59 PM 0
Share

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

}

avatar image unity_ek98vnTRplGj8Q OlgaKogiou · Mar 23, 2020 at 04:07 PM 0
Share

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).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges