- Home /
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.
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.
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613192355im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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