Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Passing Score from scene to scene

Discussion in 'Scripting' started by jankeifer, Dec 9, 2014.

  1. jankeifer

    jankeifer

    Joined:
    Jun 4, 2014
    Posts:
    14
    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
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    statics
     
  3. jankeifer

    jankeifer

    Joined:
    Jun 4, 2014
    Posts:
    14
    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
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Unity doesn't reset statics on scene load.
     
  5. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    There are plenty of ways to do this, are you planning on saving this data locally? Over a network?
     
  6. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    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.
     
  7. jankeifer

    jankeifer

    Joined:
    Jun 4, 2014
    Posts:
    14
    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..
     
  8. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    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.


    Code (JavaScript):
    1. import System.Collections;
    2. import System.Xml.Serialization;
    3. @XmlRoot("PlayerScores")
    4. public class PlayerScores extends System.Object
    5. {
    6.      public var int : currentScore;
    7. }
    The asset will do the rest, there is documentation on how to use it, although it is written in C#...
     
    jankeifer likes this.
  9. jankeifer

    jankeifer

    Joined:
    Jun 4, 2014
    Posts:
    14
    thanks to both of you. it help alot. i have solved the problem