I have a problem with regards to passing score from scene to scene. I want my games to pass scores to main scene from game scenes. the logic is Main Scenes first then load Game Scenes, Play then pass score to Main Scene after loading. Can someone please tell me how to use playerprefs in this situations? Thank you
statics
i have tried it sir, but it doesnt work. it works if it is from script to script, but from scene to scene it doesnt work. please help me, i am desperate
Unity doesn’t reset statics on scene load.
There are plenty of ways to do this, are you planning on saving this data locally? Over a network?
Thats not entirely true…it depends how he is making the script. Most people just create a Monobehavior script and slap it everywhere. If this is the case for him, when the scene loads the old object gets destroyed along with its data regardless if its static or not.
To have true static members that do not reset with scene changes is to have them internally for example creating an instance that inherits from System.Object rather than UnityEngine.Monobehavior. System.Object lives in memory until nothing points to it anymore. Unity cleans up with the Destroy() method that is invoked through reflection when the scene changes, scripts that are kept are those that implement DontDestoryOnLoad(gameObject) since the cached Monobehavior will know not to destroy the object when the scene changes.
its just locally sir Polymorphik, i just want to store my scores from the game scenes to my main scene. and i am using JavaScript…
Ah I see…I’m not too familiar with UnityScript only with C#. I do strongly recommend you switch to C# if you are in the early stages of development if not, thats cool.
I created an asset thats in the Unity Asset store that can store your data locally via XML Serialization with encryption its free. This will serialize in any language so long as you make the correct calls and type casting which is literally in 1 line of code…
Ideally what you want to do is create a class in UnityScript. Although this may seem long to you and probably over kill its standard practice to use a method like this makes it easier to maintain and expand as your game grows.
import System.Collections;
import System.Xml.Serialization;
@XmlRoot("PlayerScores")
public class PlayerScores extends System.Object
{
public var int : currentScore;
}
The asset will do the rest, there is documentation on how to use it, although it is written in C#…
thanks to both of you. it help alot. i have solved the problem