Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TEKNOPARADOX · Jun 29, 2015 at 06:15 AM · errornullreferenceexceptionconsole

Wierd Console Errors

Alright so here is my code and I am getting errors. I need help guys. Below is my code.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ScoreManager : MonoBehaviour
 {
 
     public static int score = 0;
     public Text scoreText;
     public Text bestText;
 
     void Start ()
     {
         DontDestroyOnLoad (gameObject);
         DontDestroyOnLoad (scoreText);
         DontDestroyOnLoad (bestText);
         DontDestroyOnLoad (score);
         if (score > PlayerPrefs.GetInt (Application.loadedLevel + "Best")) 
         {
             PlayerPrefs.SetInt (Application.loadedLevel + "Best", score);
             //Play new high score animation
             scoreText.text = "" + score;
             bestText.text = "" + PlayerPrefs.GetInt (Application.loadedLevel + "Best");
         }
     }
 
     void Update ()
 {
         scoreText.text = "" + score;
         bestText.text = "" + PlayerPrefs.GetInt (Application.loadedLevel + "Best");
 }
 }

Here are the errors:

MissingReferenceException: The object of type 'Text' 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. UnityEngine.EventSystems.UIBehaviour.IsActive () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/UIBehaviour.cs:22) UnityEngine.UI.Graphic.SetVerticesDirty () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Graphic.cs:93) UnityEngine.UI.Text.set_text (System.String value) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Text.cs:140) ScoreManager.Update () (at Assets/Scripts/ScoreManager.cs:29)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Jun 30, 2015 at 06:12 AM

DontDestroyOnLoad only works on root gameobjects or on components that are located on a root object. If you use it on a child object but not on the parent it have no effect at all.

That means if you load a new level the object is destroyed anyways.

If you use DontDestroyOnLoad on the root object that object and all of it's child objects won't be destroyed when you load a new level.

Comment
Add comment · Show 1 · 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
avatar image TEKNOPARADOX · Jul 01, 2015 at 12:02 AM 0
Share

Thanks so much! :D

avatar image
1

Answer by superbsumit · Jun 29, 2015 at 06:27 AM

You have not referenced your 'scoreText' and 'bestText' in you Inspector panel. Just drag n drop the respective Text file in the Inspector.

Comment
Add comment · Show 4 · 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
avatar image TEKNOPARADOX · Jun 29, 2015 at 10:25 AM 0
Share

@superbsumit thanks for your reply but it is so wierd because the error occurs even when i have already done that.

avatar image superbsumit · Jun 29, 2015 at 02:10 PM 0
Share

Where is this script attached? I think you are continuously fetching 'scoreText.text' even after you load your next scene.

The next scene will not contain the reference of your objects which are set through Inspector, even the gameObject is set to DontDestroyOnLoad.

Also, this part of your code is not valid:

 DontDestroyOnLoad (scoreText);
 DontDestroyOnLoad (bestText);
 DontDestroyOnLoad (score);

Please provide some more info on how are you using this script.

avatar image TEKNOPARADOX · Jun 29, 2015 at 10:56 PM 0
Share

@superbsumit Here is a basic rundown :

I have two scenes that utilize this scoretext and best text. The game scene and game over scene. Atm best is rendered invisible in game scene. I put dont destroy on load so I can used the same values again to render the best and score in game over. The script is attached to a game object called scoremanager in both my game scene and game over.

Thanks for the help btw

avatar image superbsumit · Jun 30, 2015 at 05:58 AM 0
Share

Ok, as per my understanding, scoremanager gameObject is set to DontDestroyOnLoad and you are getting the score in the next scene from this script only!!!

$$anonymous$$y Confusion: Is the 'scoremanager' gameObject is already present in both the scenes from start??

One simple way you can get your current score to next scene is making 'score' static and simply accessing it in other script, which you are already doing (but I don't see, that where are you setting/increasing your 'score'? It will always be zero). And you can also access the 'highScore' by using PlayerPrefs (same way you are doing). You don't really have to use DontDestroyOnLoad method. And the 'scoremanager' gameObject should only be present in the game scene.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object 0 Answers

Compiler Warnings Are Now Errors 0 Answers

How to disable specific errors in the console 1 Answer

Calling 'AddItem' Method giving me error NullReferenceException: 0 Answers

Why am I getting NullReferenceException on my .Contains()? 1 Answer


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