- Home /
Value changes after using it in another script, why?
Hello
I finally could solve my problem with playerpref, but now i have a new problem.
I have the playerpref value and want to compare it with the value i put on every "LevelImage".
But as soon i click for example on Level 3 it changes the value 4 to 7. i put everthing into the console so i can see whats happening with my values.
here are the two scripts.
public class AktuellesLevel : MonoBehaviour {
public int Level;
// Start is called before the first frame update
void Start()
{
Level = GetComponent<AktuellesLevel>().Level;
}
// Update is called once per frame
void Update()
{
}
public void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log(Level);
}
}
And here the second
public class LoadManager : MonoBehaviour { private string LevelChoice; private AktuellesLevel AL; public int aktuellesLevel;
// Start is called before the first frame update
private void Start()
{
AL = FindObjectOfType<AktuellesLevel>();
aktuellesLevel = PlayerPrefs.GetInt("farthestLevel");
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
if (AktuellesLevel > AL.Level)
{
Select();
}
else
{
Debug.Log("Vorheriges Level noch nicht abgeschlossen");
}
}
}
public void Select()
{
Debug.Log(AL.Level);
Debug.Log(AktuellesLevel);
SceneManager.LoadScene(LevelChoice);
}
public void OnTriggerEnter2D(Collider2D collision)
{
LevelChoice = collision.name;
}
public void Hauptmeue()
{
SceneManager.LoadScene(0);
}
In the second script i shouldt even be able to choose level3 but it is still loading. And after loading the value AL.Level is 7, not 4 how i set it in the inspector.
I hope someone can help me. I try for a long time now to make to player play each level after another, i seems really complicated to me.
Thanks
Answer by Megaboy238 · Feb 14, 2021 at 08:53 PM
You are passing a string to the SceneManager so it will be selecting by Scene Name not index number, check the names are correct in File > Build Settings
Hello
Thanks for your input. I know about the string, but the select method shouldnt run if the two values i try to compare are not equal.
But now while i am typing this, i could just change the string into a value and compare this with playerpref...ill try that, the value just needs to be the same like in the build settings
Thanks for triggering my brain :)
Your answer
Follow this Question
Related Questions
save game using player prefs 2 Answers
HOW to PlayerPrefs.DeleteAll () in that only level ? HELP 0 Answers
Why this middle code doesnt want to run, Playerpref problem 0 Answers
Leveling up and Unlockables 0 Answers
Storing in PlayerPref based on timer 1 Answer