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 /
  • Help Room /
avatar image
0
Question by oscar_wong67 · Oct 05, 2015 at 11:58 PM · variableloadingscenesscoring

Object Reference not set to an instance of object / printing a variable from another scene

Hello!

I'm essentially trying to get the score (# of flips) from this script:

  #pragma strict
  
  var flips = 0;
  
  var facingUp = true;
  
  function Update() {
  
      if (facingUp) {
          if (Vector3.Dot(Vector3.up, transform.up) < -0.75) {
              facingUp = false;
              flips++;
          }
      }
      else {
          if (Vector3.Dot(Vector3.up, transform.up) > 0.75) {
              facingUp = true;
              flips++;
          }
      }
 
      Debug.Log(flips);
      
  }
 

and print it on the screen during the "game over" scene, but I don't know how to get the variable to carry over. On top of that, I'm getting an error with my text on the "game over" scene:

"Object reference not set to an instance of an object", which appears to be due to my ToString() method in the following code:

 #pragma strict
 
 var go : pizza;
 var text : UI.Text;
 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;
 }

I have tried to use DontDestroyOnLoad in the first script, but I assume that it doesn't work since there's an error with my text on the second script? I'm really unsure how to go about it, any help is appreciated. Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Statement · Oct 10, 2015 at 02:58 PM

The error "Object reference not set to an instance of an object" does not have anything to do with calling int.ToString() as part of the expression go.flips.ToString();. The only possible object reference in that expression is go, meaning the expression go.flips fails because go is set to null.

But let's step back a little to get an overview. You seem to have a pizza object in your game scene and then some UI presentation object in your gameover scene. It sounds like you want the pizza object to carry over into the gameover scene but you are not telling the pizza object to DontDestroyOnLoad. You are telling the UI presentation object that. If you wanted your pizza object to carry over to the next scene, you should move the call of DontDestroyOnLoad to the pizza script.

However, it could be easier to just declare a static variable which is accessible regardless if the pizza object lives or not.

pizza.js

 #pragma strict
   
 static var flips = 0;      
 var facingUp = true;
 
 function Awake() {
     flips = 0; // Since it is a static var, we want to reset it every time we play or it will keep adding between games
 }

 function Update() {      
     if (facingUp) {
         if (Vector3.Dot(Vector3.up, transform.up) < -0.75) {
             facingUp = false;
             flips++;
         }
     }
     else {
         if (Vector3.Dot(Vector3.up, transform.up) > 0.75) {
             facingUp = true;
             flips++;
         }
     }     
     Debug.Log(flips);
 }

pizzaPresenter.js

 #pragma strict
  
 var text : UI.Text;
  
 function Update () {
     text.text = "Score: " + pizza.flips.ToString();
 }
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 oscar_wong67 · Oct 10, 2015 at 10:06 PM 0
Share

I was able to get the score to on the game over scene, yet I still get the error. The instance of

 scoring = GameObject.Find("text").GetComponent.<ScoreText>();

for the "Game Over scene" gives the error, but is still able to print the score using the data from it.

Everything appears to function, but I'm still getting the error.

$$anonymous$$y pizza.js is currently the same as what you posted. $$anonymous$$y Last page script is:

 #pragma strict
 var scoring : ScoreText;
 var endText : UI.Text;
 
 scoring = GameObject.Find("text").GetComponent.<ScoreText>();
 function Start () {
     
     
 }
 
 function Update(){
 
     endText.text = scoring.printer;
 
 }

$$anonymous$$eanwhile, the script that prints the score live during the game itself is:

 #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;
 }

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

Complicated level change problem 1 Answer

How to predict/Estimate scene loading time before loading the scene?,A quick and/or reliable way to predict/estimate scene loading time? 0 Answers

Saving and loading an int variable on Android 1 Answer

How to make a global variable in Unity? 5 Answers

Generate a variable for each object that collides 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