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

Remembering coordinates from previous level.

Discussion in 'Scripting' started by TheWgames, Sep 4, 2013.

  1. TheWgames

    TheWgames

    Joined:
    Nov 29, 2012
    Posts:
    27
    Hi,
    I am trying to find a script to make the level change into another scene which looks a little bit different. That works out, but I want to change my character into another one. I just grab another character and put it into the 2nd scene. But the problem is that I don't know where the characters last position was. I had an idea for a script that keeps the coordinates of the character, and with the use of don'tdelete the script ( object with script) would come into the 2nd level. Maybe another script in the 2nd level can use the var of the coordinates, and warp the new character to it's old position.

    I am not very good in scripting, but I don't have the time to figure this out by myself. I tried to find something, but I really couldn't find anything.

    It would be really great if somebody could help me with this, and I will really appreciate it.

    Thanks,
    Willie
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    There are 2 ways: use PlayerPrefab class or implement singleton class to store data bedween scenes. In my opinion 2nd way is better.

    Simplest implementation os singleton pattern bellow:
    *UPD*
    Code (csharp):
    1.  
    2.     class myData
    3.     {
    4.        private static myData instance = null;
    5.        public static myData Instance
    6.        {
    7.           get
    8.           {
    9.             if(instance == null)
    10.                 instance = new myData();
    11.             return instance;
    12.           }
    13.        }
    14.        public Vector3 LastPosition;
    15.        
    16.        private myData()
    17.        {
    18.             ;
    19.        }
    20.     }
    21.      
    22.     ...
    23.     /// usage Scene 1
    24.     myData.Instance.LastPosition = player.transform.position;
    25.      
    26.     /// usage Scene 2
    27.     player.transform.position = myData.Instance.LastPosition;
    28.  
     
    Last edited: Sep 4, 2013
  3. TheWgames

    TheWgames

    Joined:
    Nov 29, 2012
    Posts:
    27
    Thank you very much!
    Do I need to add the mydata script and the usage scene 1 to the character in the 1st scene, and the usage 2 to the char in the 2nd scene? Or how does that work?

    Thanks,
    Willie
     
  4. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    No, you should not add this script to any game object, it is not MonoBehaviour succesor. Just place this file at your project.
    UPD: I change code example, there was some errors, sorry)))
     
  5. TheWgames

    TheWgames

    Joined:
    Nov 29, 2012
    Posts:
    27
    I placed the script in my standard assets, but I got a lot of errors.
    Unexpected token: myData
    Unexpected token: Vector3

    Could you help me with this?
     
  6. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Hey, you should not put ALL this code in ONE file.

    This is should be placed in one file:
    Code (csharp):
    1.  
    2.         using UnityEngine;
    3.         class myData
    4.         {
    5.            private static myData instance = null;
    6.            public static myData Instance
    7.            {
    8.               get
    9.               {
    10.                 if(instance == null)
    11.                     instance = new myData();
    12.                 return instance;
    13.               }
    14.            }
    15.            public Vector3 LastPosition;
    16.            
    17.            private myData()
    18.            {
    19.                 ;
    20.            }
    21.         }
    22.  
    But this is example of how to save and read data from any place in your game where you want to do this:

    Saving:
    Code (csharp):
    1.  
    2.         myData.Instance.LastPosition = player.transform.position;
    3.  
    Reading:
    Code (csharp):
    1.  
    2.         player.transform.position = myData.Instance.LastPosition;
    3.  
    P.S.: "Unexpected token: Vector3" error occurs because you need to add using UnityEngine; directive in the begining of the file (I added it in this example).

    Good luck!