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 oscar_wong67 · Oct 24, 2015 at 01:32 PM · errorgameobjectvariablevalue

Object not set to instance of an object

Hi everyone,

I keep getting this error, even though I don't know that it's actually causing problems:

 NullReferenceException: Object reference not set to an instance of an object
 ScoreLastPage.Main () (at Assets/ScoreLastPage.js:7)

Which is to do with this script, which prints the most recent score and the high score on the "game over" page:

 ScoreLastPage.js
     #pragma strict
 public static var scoring : ScoreText;
 var endText : UI.Text;
 var highScoreText : UI.Text;
 var highScore : int;
 
 scoring = GameObject.Find("ScoringText").GetComponent.<ScoreText>();
 
 function Awake() {
     DontDestroyOnLoad(transform.gameObject);
 }
 
 function Start () {    
 
     Debug.Log("High Score: " + highScore);
 }
 
 function Update(){
 
     
     endText.text = scoring.printer;
 
     //BUG: high score doesn't update.
     
     highScoreText.text = "High Score: " + highScore;
 
     if (parseInt(scoring.go.flips) > highScore) {
         PlayerPrefs.SetInt("highScore1", scoring.go.flips);
         PlayerPrefs.Save();
         highScore = PlayerPrefs.GetInt("highScore1");
         
     }
     else {
         highScore = PlayerPrefs.GetInt("highScore1");
     }
     highScoreText.text = "High Score: " + highScore;
 
 }

It draws from this script to get the current score (ScoreText.js):

 #pragma strict
 
 var go : pizza;
 var text : UI.Text;
 public static var printer : String;
 
 function Awake() {
     DontDestroyOnLoad(transform.gameObject);
 }
 
 function Start () {
     go = GameObject.Find("pizza-small").GetComponent(pizza);
 
 }
 
 function Update () {
     printer = go.flips.ToString();
     text.text = "Score: " + printer;
 }

That script takes the tracked number of flips from a script called "pizza.js", and prints the score live, during the gameplay. Also, ScoreLastPage.Js has issues with my playerprefs and how I save them, so my questions are:

  • How can I get rid of my aforementioned error?

  • If the player gets a new score, that is overwritten as the high score, even if the value is not higher than the previous high score. For instance, if the high score is 4, and I get 1 point, the high score is set to 0.

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
0

Answer by AshleyJamesy · Oct 25, 2015 at 05:05 PM

You should always initiate variables in the start or awake functions.

The reason you're getting that error is because the game object you're trying to reference in your script is null.

 ScoreLastPage.js
 #pragma strict
      public static var scoring : ScoreText;
      var endText : UI.Text;
      var highScoreText : UI.Text;
      var highScore : int;
      
      function Awake()
 {
     DontDestroyOnLoad(transform.gameObject);
 }
 
 function Start()
 {
     scoring = GameObject.Find("ScoringText").GetComponent.<ScoreText>();
 
     if (scoring == null)
     {
         Debug.Log("Unable to find ScoringText");
         return;
     }
 
         Debug.Log("High Score: " + highScore);
 }
 
 function Update()
 {
     if (scoring != null)
     {
         endText.text = scoring.printer;
 
         highScoreText.text = "High Score: " + highScore;
 
         if (parseInt(scoring.go.flips) > highScore)
         {
             PlayerPrefs.SetInt("highScore1", scoring.go.flips);
             PlayerPrefs.Save();
             highScore = PlayerPrefs.GetInt("highScore1");
         }
         else
             highScore = PlayerPrefs.GetInt("highScore1");
 
         highScoreText.text = "High Score: " + highScore;
     }
 }
Comment
Add comment · Show 3 · 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 Derpyness · Oct 25, 2015 at 11:29 PM 0
Share

Remember to replace

scoring = GameObject.Find("ScoringText").GetComponent.();

with

scoring = GameObject.Find("ScoringText").GetComponent();

avatar image oscar_wong67 · Oct 26, 2015 at 01:29 AM 0
Share

I tried that, but the Debug.Log's don't return null, so they have a value, even though I get the error, there's something wrong w/ GetComponent for sure.

avatar image Derpyness oscar_wong67 · Oct 26, 2015 at 10:30 AM 0
Share

Oh sorry, Unity Answers doesn't like pointy brackets, just replace the square ones with pointy ones.

scoring = GameObject.Find("ScoringText").GetComponent[ScoreText]();

avatar image
0

Answer by oscar_wong67 · Oct 26, 2015 at 12:02 PM

Alright I solved it, and I'll leave this for future reference of anyone searching:

I just redid the scoring code completely and made a "Game Manager" prefab with a game manager script on it, made the score a static variable, and passed everything from the game manager script.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Store previous gameObject that raycast had hit. (Or store any previous value in Update for that matter) 1 Answer

How to change variables on a new gameobject's script 1 Answer

Expression denotes a 'type' 2 Answers

Getting error: Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable 1 Answer

Public variable in script different for every game object. 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