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 SUPPEAR · Apr 06, 2015 at 09:26 PM · android2dgameobjectscorelevel

OnLevelWasLoaded script problem.

Hello. I am making a game that is 2D and for Android. I am having a problem where my score won't completely transfer through a scene ( Using don'tdestroyonload doesn't fix it ) The script I use only updates the score when a point is awarded. So I went through some looking and found OnLevelWasLoaded. How can I use OnLevelWasLoaded to update the score/text by basically adding no points ( For example; adding 0 points, which I hope updates the text to display the score )

The scoremanager script is

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ScoreManager : MonoBehaviour
 {
     public static int score; // The player's score.
     Text text; // Reference to the Text component.
     
     void Awake ()
     {
         text = GetComponent <Text> ();
     }
     
     public void SetScore (int newScore)
     {
         ScoreManager.score = newScore; //update the static score variable
         text.text = "Score: " + score;
         Debug.Log;
     }
 }

The script that adds points is

 using UnityEngine;
 using System.Collections;
 
 public class Score : MonoBehaviour {
     public int scoreValue = 1;
     public ScoreManager scoreManager;
     
 
     
     void OnMouseDown () {
         scoreManager.SetScore(ScoreManager.score + scoreValue);
     }
 }

and I want to use

 function OnLevelWasLoaded (level : int) {
         if (level == 13) {
             print ("Woohoo");
         }
     }


I want to use that ^^^^ by somehow converting it to add no points ( Which I'll repeat that I hope will update the text )

The reason I am posting this is because I do not know how to convert it at all, I am extremely new to scripting and game creating.

I have tried doing it and I will show what I have done

 using UnityEngine;
 using System.Collections;
 
 public class aed : MonoBehaviour {
     public int scoreValue = 1;
     public ScoreManager scoreManager;
     
     
     void  OnLevelWasLoaded ( int level  ){
         if (level = "EndGame") {
             scoreManager.SetScore(ScoreManager.score + scoreValue);
         }
     }
 }


I do not know if this would even work but there is an error saying

Assets/Standard Assets (Mobile)/Textures/aed.cs(10,21): error CS0029: Cannot implicitly convert type string' to int'

I have no clue how to fix that which is why I am posting this.

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
Best Answer

Answer by MrSpoke · Apr 07, 2015 at 12:32 AM

First, your variable level is type integer and you are comparing it to a string. That can't happen. You could choose what to do from these two:

 if (level == 1) // for example, replace the 1 by the number of the level

Don't forget the double equals to compare things. This way you are comparing the integer level to an integer, which is what you should do. Or, you could change the type of the variable.

 void OnLevelWasLoaded (string level)
 {
     if (level == "EndGame") 
     {
         scoreManager.SetScore(ScoreManager.score + scoreValue);
     }
 }

Hope this helps.

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 SUPPEAR · Apr 07, 2015 at 01:03 AM 0
Share

From my basic understanding, that sort of made sense. I chose the second choice but this error comes

Script error: OnLevelWasLoaded This message parameter has to be of type: The message will be ignored.

avatar image MrSpoke · Apr 07, 2015 at 01:24 AM 0
Share

That was my mistake. Sorry about it. You can't use the second option because the parameter of the function OnLevelWasLoaded is an int. So just disregard the second and use the first, it should work.

avatar image SUPPEAR · Apr 07, 2015 at 02:03 AM 0
Share

Thank you so much. It now works :D

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

19 People are following this question.

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

Related Questions

Score system 1 Answer

Carry over texts from one scene to another? 1 Answer

Duplication Problem. Please help. 1 Answer

Expand object on touch? 1 Answer

Transform.LocalScale Script Problem. Please help. 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