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 CallToAdventure · Apr 01, 2012 at 04:28 PM · scorelevel load

Simple score question

Here's what I want to do; achieve this score (say 100) and load next level. And here's my script:

 #pragma strict
 var score : int =0;
 function Start () 
 {
     GetComponent(TextMesh).text = PlayerPrefs.GetInt("score", 0).ToString();
 }
 
 function Update ()
 
 {
 
 if(score == 100){
 Boo();
 
 }
 
 }
 
 function Boo(){
     Application.LoadLevel("Level2"); 
 
 }

I'm sure it's something dead simple but i am really new to this.

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 farooqaaa · Apr 01, 2012 at 04:49 PM 0
Share

You have never assigned the score variable or I'm missing something?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Apr 01, 2012 at 04:47 PM

I don't know exactly what's going wrong in your case, but you should save the score value read in the variable score, or it will be only appear in the 3D text:

function Start () 
{
    score = PlayerPrefs.GetInt("score", 0); // save the value in score
    GetComponent(TextMesh).text = score.ToString(); // then copy it to the text mesh
}
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 CallToAdventure · Apr 01, 2012 at 04:56 PM 0
Share

I get errors on score=Play... line.

Assets/Scripts/score.js(9,9): BCE0049: Expression 'score' cannot be assigned to.

and

Assets/Scripts/score.js(10,41): BCE0020: An instance of type 'UnityEngine.Object' is required to access non static member 'ToString'.

avatar image aldonaletto · Apr 01, 2012 at 05:04 PM 0
Share

Have you changed something in the code you've posted? The variable score is correctly declared in the script posted, thus these errors should not occur (they seem to indicate that score has not been declared yet).

avatar image CallToAdventure · Apr 01, 2012 at 06:45 PM 0
Share

I don't get the errors anymore - but I don't get any other result either. Here's the updated script.

 var score : int =0;
 function Start () 
 {
     score = PlayerPrefs.GetInt("score", 0); // save the value in score
     GetComponent(Text$$anonymous$$esh).text = score.ToString();
 }
 
 function Update ()
 
 {
 
 if(score == 100){
 Debug.Log("Score is 100");
 Boo();
 
 }
 
 }
 
 function Boo(){
     Application.LoadLevel("Level2"); 
 
 }
avatar image aldonaletto · Apr 01, 2012 at 08:22 PM 0
Share

But which results do you expect? The score value is loaded at Start, and nothing in this script is changing it. Unless other scripts increment the variable score (or if its value in PlayerPrefs is 100) the Level2 will never be loaded. You should also move the assignment to Text$$anonymous$$esh.text to the Update function, so any score change would appear in the text:

function Update ()
{
  getComponent(Text$$anonymous$$esh).text = score;
  if(score >= 100){ // handles score >= 100 case
    Debug.Log("Score is >= 100");
    Boo();
  }
}

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

6 People are following this question.

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

Related Questions

Scoreboard will not update 1 Answer

Don't destroy script on load 2 Answers

Money System Not Working Please Help!!!! (Code fully commented!) 3 Answers

When should I save data 2 Answers

Does any know how to hide and show 2d sprites in unity 5 by using a 2d boxcollider set to trigger when player collides with the boxcollider 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