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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Going from Town to Battle to Town

Discussion in 'Scripting' started by malialis, Feb 1, 2016.

  1. malialis

    malialis

    Joined:
    Feb 4, 2015
    Posts:
    24
    I'm making a turn-based battle game in Unity3D and I have 2 scenes (a town scene and a battle scene). Whenever the character meets a monster, the game jumps to the battle scene where the character does battle and go back to the town scene after defeating or losing.

    The problem is how to return to the town scene with the same state as when the character enters the battle scene (character position, statuses, etc.)

    if I use

    Application.LoadLevel('battlescene');

    then

    Application.loadLevel('townScene');

    then the townScene will start from the first time. How to make the townScene continue from where we left off?
     
  2. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Look at DontDestroyOnLoad. It keeps things safe from scene changes and such, but you will probably have to rethink how your character works to make it how you want it... or make character stats and such separate from the physical char in the scenes.
     
    malialis likes this.
  3. malialis

    malialis

    Joined:
    Feb 4, 2015
    Posts:
    24
    Hello.

    Thank you for the quick reply.

    I will look more at DontDestroyOnLoad.Would my guy still start at the start point and not where he was upon entering battle. I have playerPrefs for some of the status. I wonder if a checkpoint system would work for saving a new temp startPoint?

    I could be way off on this. Thank you very much again.
     
  4. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Well there are 2 main ways to do this.

    If you don't destroy your character, you will need to set up some spawn points in your scenes where you can set your character's location to when whatever happens. You whole character remains between scene loads and such.

    If you pull the character script off of your character (and put a reference to the actual character to do whatever with)... and just have it on an empty game object that isn't destroyed... then you can change / load scenes and spawn your character however and just assign it to the script
     
    malialis likes this.
  5. malialis

    malialis

    Joined:
    Feb 4, 2015
    Posts:
    24
    Hello.

    Thank you very much. I will give this a go when I get home tonight. This is really helpful and is pointing in the right direction.

    Thank you again.