Hi,
GetUpKidAK kindly suggested I should pass my values to another script for my game, and pass them back when the level is reset. Which is a very good idea. I have been trying to get it to work, but my lack of understanding for c# and coding in general is still holding me back :(.
Before I call Application.LoadLevel (“SideSpikeSlice”); to reset the level. I am trying to set the stats values from my object. To do this I have set up a class called PlayerStats.cs as well my my original class PlayerController.cs.
In PlayerController.cs I set the following:
private PlayerStats playerStats;
void Start () {
current = this;
playerPhysics = GetComponent ();
playerStats = GetComponent();
}
I call the function via:
playerStats.GetComponent().setStats();
Application.LoadLevel (“SideSpikeSlice”); //here is where the level resets
This is what the function being called looks like:
public void setStats(){
score = PlayerController.current.score;
}
I have also tried calling the function as PlayerStats.current.setStats(); as well as a variety of other ways people have explained it from google searches, none of them seem to work. I use the .current to get past the accessing non-static error that you receive if you don’t use is.
The PlayerController.cs script is attached to my player prefab, but my PlayerStats script is not attached to anything, as I just want it to store values. However, whenever I call the function it says:
NullReferenceException: Object reference not set to an instance of an object
PlayerController.Update () (at Assets/Scripts/PlayerController.cs:203), which is the line where I call the function to setStats();
I have the option to attatch sometihng to “Player Stats” in the inspector:
http://puu.sh/djEfU/8d73615276.png
But I can’t drag PlayerStats.cs in there :(.
I can’t attach the script to my player object, as when I reload the level it would then reload the script with the stats on it, as far as I understand. My player object does have a field for “Player Stats”, but I am unable to drag the PlayerStats script in there. I’m sure I’m being incredibly stupid, I just can’t seem to find an answer that works.
Cheers
Lucio