Unity C# Can't update variables in other scripts

Hello!

I have now been sitting for the last two hours trying to figure out how I can solve this problem of mine. You see, I am currently making a game where the purpose of the game is to make the player able to select his level - depending on which levels he has unlocked. So basically I have set a trigger for the “teleportation object”, with name “tp1”. I know you may find some of my code rather odd, but that’s not the problem. The problem is to access the class.

My script is as follows;

public PlayerClass pc;

void Start() {
pc = new PlayerClass();
}

void OnMouseOver() {
if(this.gameobject.name == "tp1" && gameisTrigger == true && hasuLockedlvl1 == true) {
//teleport player
}
}

Even though I play and “unlocks level 1”, and updates the variable from another script it doesn’t seem to update here.

And another script I have the following when the player actually unlocks the level 1;

public PlayerClass pc;
void Start() {
pc = new PlayerClass();
}

void Update() {
if(playerhasDoneSomething == true) {
pc.level = 2;
}
}

The problem is that it doesn’t seem to update the class, so if I call a debug after I have changed the value inside the class - it is updated. However, the other object that manages the “access” to the other levels doesn’t seem to update.

Here is my PlayerClass by the way;

private int level;

public int Level {
	get{return level;}
	set{level = value;}
}

I would really appreciate if some of you could help me out with this problem.
Thanks in advance.

The top 2 code snips are from different scripts?

You make a new PlayerClass object in each of them, so the PlayerClass object in your second code snip is different from the one you have in your first one, because you have 2 PlayerClass objects.

Maybe you coudl use PlayerPrefs?
Or maybe PlayerClass should be a static class, or implement a singleton pattern? The thing is you need to make sure you’re accessing the same object in your scripts.