• 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 Shantred · Apr 28, 2012 at 01:36 AM · scenegetcomponent

Accessing Components in Other Scenes

I'm working on a project where a game is played on one scene and on another scene we've got the "score" screen. The score screen should display the score obtained on the game scene.

I've got the score kept track of in a script called scriptPlayer. The score works correctly, however I can't seem to properly access the score on the score screen. I get the following error over spamming the console:

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

scriptScreenWin.OnGUI () (at Assets/Scripts/scriptScreenWin.js:15) I've tried accessing the component in the following way(s) var playerInfo = gameObject.GetComponent(scriptPlayer); var playerInfo = GetComponent(scriptPlayer); var playerInfo = GetComponent("scriptPlayer"); I've tried various other similar ways, but cannot get it to access when using GUI.Label(Rect(10,50, 100, 40), "You got" + playerInfo.score + "points!");
This is beyond frustrating, since as far was what I've been able to look up seems correct.
Comment
Add comment · Show 1
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 Lo0NuhtiK · Apr 28, 2012 at 02:26 AM 0
Share

I edited your tags : Just for future reference, "wtf" isn't a tag that should be used.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Berenger · May 02, 2012 at 02:51 AM

There is three easy ways to achieve that :

  • With static variable, which exist through all and as long as the application.

  • By saving it on the hard drive, with PlayerPrefs as stated by Nooch.

  • By accessing the instance of an object keeping track of the score and having executed DontDestroyOnLoad(), as stated by Lo0NuhtiK.

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 syclamoth · May 02, 2012 at 02:52 AM 0
Share

$$anonymous$$ethod 3 is by far the best one, though.

avatar image
0

Answer by Nooch · Apr 28, 2012 at 02:01 AM

Could you save/retrieve the score using PlayerPrefs? It's trivial to write and read data stored there, from any scene.

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 Shantred · Apr 28, 2012 at 02:05 AM 0
Share

Being that the game scene is the main scene for the project and intended to be played over and over, is that the best route?

avatar image Lo0NuhtiK · Apr 28, 2012 at 02:10 AM 0
Share

Try making an empty gameObject with DontDestroyOnLoad() on it, and put the score script on that object. Then when you switch scenes that object will carry over into the next scene, and you can access it's script like you've been trying to.

avatar image Nooch · May 02, 2012 at 02:35 AM 0
Share

Both methods would work fine. I would keep the score in a local variable during play, then write it out to the Playerprefs at the end of the scene. Then just read it once when the next scene starts. Easy... and the data remains between game sessions - so it's a handy way to keep a high score, etc.

avatar image
0

Answer by joelm27 · Mar 06, 2016 at 11:34 AM

You can use PlayerPrefs to save score from scene one and display it on scene 2.

Check this url: http://answers.unity3d.com/questions/530444/how-to-add-and-keep-score-with-playerprefs-for-the.html

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

reference an object in a diference scene 0 Answers

Connection between menu and scene 2 Answers

Cannot see anything when press ' Play' 1 Answer

Loading player location in a scene 2 Answers

Put a map into Unity 0 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