Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » Saving and Loading Problems ( The whole problem will be stated inside the post)

Saving and Loading Problems ( The whole problem will be stated inside the post)

View previous topic View next topic Go down  Message [Page 1 of 1]

GX@Infiniti


Member [Lv. 1]
Member [Lv. 1]
Okay, I'm still a newbie in Unity. Not trying to be irritating here, but first, look at my problem.

The whole thing begins at watching this tutorial: [MEDIA=youtube]qwuPiaFU37w[/MEDIA]
At first, there's not much problem while watching the first part until the ninth part. Then, when I watched till the last part, I decided to try my game (largely based on that tutorial). When I click "play" in my project, I went into the main menu and start a new game. After I completed the first level, the winning message appears in my screen, but it states that I have completed "level 0" : http://prntscr.com/9z8a1g

I ignored the issue anyways and clicked " continue" and went into the next level. Then, I finished that level too but, the winning message gives me the same thing, it still said that I completed level 0 though it's level 2: http://prntscr.com/9z8dsv

What's wrong was even after I clicked "continue", it still spawns me at the same level, level 2, again and again. I quit that level and went back to the main menu, and I clicked "continue" on the menu, which leads me to the level select menu. ( I followed the tutorial and made the "level locked" system in the game ), I found out that though level 1 was unlocked, as i made it to be unlocked by default, ( link to pic: http://prntscr.com/9z8q58 ), level 2, though completed, was locked. ( link to pic:  http://prntscr.com/9z8qn6 )

Therefore, I need a solution for that, where I need the winning message to show correctly according to that level, and be able to continue to the next level without any problems (and able to save properly, by unlocking the next level and save my progress)

Here are the scripts related to the problem.

Code:
using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {


//count
    public int currentScore;
    public int highscore;
    public int currentLevel = 0;
    public int unlockedLevel;
    public int tokenCount;
    private int totaltokenCount;

//timer variables
    public float startTime;
    private string currentTime;
    public Rect timerRect;
    public Color warningColorTimer;
    public Color defaultColorTimer;

//GUI Skins
    public GUISkin skin;
//references
    public GameObject tokenParent;
    private bool completed = false;
    private bool showWinScreen = false;
    public int winScreenWidth, winScreenHeight;




    void Update()
    {

        if(!completed)
        {

        startTime -= Time.deltaTime;
        currentTime = string.Format("{0:0.0}", startTime);
        if(startTime <= 0)
        {
            startTime = 0;
            //Application.LoadLevel("Main Menu");
        }
        }

    }

    void Start()
    {
        totaltokenCount = tokenParent.transform.childCount;
        if(PlayerPrefs.GetInt("Level Completed") > 1)
        {
         currentLevel = PlayerPrefs.GetInt("Level Completed");

        }else
        {
            currentLevel = 0;
        }
        //DontDestroyOnLoad(gameObject);
    }

    public  void CompleteLevel()
    {
        showWinScreen = true;
        completed = true;
    }

    void LoadNextLevel()
    {
        Time.timeScale = 1f;
        if (currentLevel < 3)
        {
        currentLevel += 1;
        print (currentLevel);
        SaveGame();
        Application.LoadLevel(currentLevel);
        }
        else{

            print("You Win!!!");
    
             }
    }

    void SaveGame()
    {


        PlayerPrefs.SetInt("Level Completed", currentLevel);
        PlayerPrefs.SetInt("Level" + currentLevel.ToString() + "Score", currentScore);
    }



    void OnGUI()
    {
        GUI.skin = skin;

        if (startTime < 5f)
        {
            skin.GetStyle("Timer").normal.textColor = warningColorTimer;
        }
        else{
            skin.GetStyle("Timer").normal.textColor = defaultColorTimer;
            }

        GUI.Label (timerRect, currentTime, skin.GetStyle("Timer"));
        GUI.Label (new Rect(45,100,200,200),tokenCount.ToString() + "/" + totaltokenCount.ToString());


        if(showWinScreen)
        {
            Rect winScreenRect = new Rect(Screen.width/2 - (Screen.width*.5f/2), Screen.height/2 - (Screen.height*.5f/2), Screen.width*.5f , Screen.height*.5f);
            GUI.Box(winScreenRect, "Yeah");
        
            int gameTime = (int)startTime;
            currentScore = tokenCount * gameTime;
            if(GUI.Button(new Rect(winScreenRect.x + winScreenRect.width - 170, winScreenRect.y + winScreenRect.height -60, 150, 45), "Continue"))
            {LoadNextLevel();}
            if(GUI.Button(new Rect(winScreenRect.x + 20, winScreenRect.y + winScreenRect.height -60, 100, 45), "Quit"))
            {
                Application.LoadLevel("Main Menu");
                Time.timeScale = 1f;

             }

            GUI.Label(new Rect(winScreenRect.x + 20, winScreenRect.y + 40, 300, 250), "Score: " + currentScore.ToString());
            GUI.Label(new Rect(winScreenRect.x + 20, winScreenRect.y + 70, 300, 250), "Level " + currentLevel.ToString() + " Completed");

        }




    }


    public  void AddToken()
    {
        tokenCount += 1;
    
    }


}

Code:
using UnityEngine;
using System.Collections;

public class LevelLoader : MonoBehaviour {
      public int levelToLoad;
      private string loadPrompt;
      private bool inRange;
      private int completedLevel;
      private bool canLoadLevel;
      public GameObject padlock;

      void Start()
      {
          completedLevel = PlayerPrefs.GetInt("Level Completed");

        if (completedLevel == 0)
        {
          completedLevel = 1;


        }
    
          canLoadLevel = levelToLoad <= completedLevel ? true: false;
    
    
        if(!canLoadLevel)
        {
            Instantiate(padlock, new Vector3(transform.position.x , 0f, transform.position.z - 2f ), Quaternion.Euler (0,90,0) );

       }    


      }

      void Update()
     {

        if(canLoadLevel && Input.GetButtonDown("Action") && inRange)
         {
             Application.LoadLevel("Level " + levelToLoad.ToString());

         }

     }

      void OnTriggerStay(Collider other)
     {
         inRange = true;
         if(canLoadLevel)
         {
         loadPrompt = "Press [E] to load Level " + levelToLoad.ToString();
         }
         else
         {
         loadPrompt = "Level " + (levelToLoad).ToString() + " is locked.";

         }



     }

     void OnTriggerExit()
     {
         loadPrompt = "";
         inRange = false;
     }




     void OnGUI()
     {
         GUI.Label (new Rect(10, Screen.height * 0.9f, 200,40), loadPrompt);



     }

 

}

Code:
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public GameManager manager;
    public float moveSpeed;
    private float maxspeed = 5f;
    private Vector3 input;
    private Vector3 spawn;
    public GameObject deathParticles;
    public bool usesManager = true;

    public AudioClip[] audioClip;


    // Use this for initialization
    void Start () {

        spawn = transform.position;
        if(usesManager)
        {
        manager = manager.GetComponent<GameManager>();
        }
    }

    // Update is called once per frame
    void FixedUpdate () {
        input = new Vector3(Input.GetAxisRaw("Horizontal"),0, Input.GetAxisRaw ("Vertical"));

        if(rigidbody.velocity.magnitude < maxspeed)
        {

        rigidbody.AddRelativeForce(input * moveSpeed);
        }

        if(transform.position.y < -2)
        {
            Die();
        }

        Physics.gravity =Physics.Raycast(transform.position, Vector3.down, .1F) ? Vector3.zero : new Vector3 (0,-9.5f,0);


    }

    void OnCollisionEnter(Collision other)
    {
        if(other.transform.tag == "Enemi")
        {
        // Instantiate is a method used to create an instance of an object at a certain position in the world
        Die();
        PlaySound(1);

        }

    }
    void OnTriggerEnter(Collider other)
    {
        if(other.transform.tag == "Goal")
        {
        manager.CompleteLevel();
        PlaySound(2);
        Time.timeScale = 0f;

        }

        if(other.transform.tag == "Enemi")
        {
         Die();
         PlaySound(1);

        }


        if(other.transform.tag == "Coins")
        {
            if(usesManager)
            {
            manager.tokenCount += 1;
            }
            Destroy(other.gameObject);
            PlaySound(0);
        }
    }


    void Die()

    {
    Instantiate(deathParticles, transform.position, Quaternion.Euler(270,0,0));
            transform.position = spawn;

    }


    void PlaySound(int clip)
    {
        audio.clip = audioClip[clip];
        audio.Play();
    }




}

Or if you don't mind, I can just upload the whole project to you if you want to look for further details.

https://www.mediafire.com/?6qdxp323dpm6gfm



Yeah sorry if I suck in explaining.

BTW I am using Unity 4.3.4 for this. I need replies for this topic asap as people from the Unity forums are just... cold about help topics IMO.

View user profile

QuickTricky


Member [Lv. 2]
Member [Lv. 2]
void Start()
   {
       totaltokenCount = tokenParent.transform.childCount;
       if(PlayerPrefs.GetInt("Level Completed") > 1) <<<< Should be 0
       {

you might double check this but, I think this here is your only problem. At least it's all I really saw from a quick scan.

View user profile

GX@Infiniti


Member [Lv. 1]
Member [Lv. 1]
Oh now I realize, how careless of me. Razz
Thanks anyway.

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum