Search Unity

Multiplayer game

Discussion in 'Multiplayer' started by gk104, Sep 26, 2014.

  1. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    I want to develop a simple 2d online game in unity, the problem is moving information between scenes,
    for example, im currently playing on server X(scene 1 in unity) and I have gained y XP, then how should I show the XP on the main menu(scene 0)?

    maybe at the end of the round, should I update the database and then retrieve data from the database in the main menu? sorry for my ignorance, i never developed any online game.
     
  2. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,315
    imagine you have a function: void GameIsfinished() inside you can put your save stuff,then when save is done you can load the menu scene.
    saving data is not related to networking if you use unity network or photon for example.
    you can save using playerprefs ( only local save), using database or using a cloud server

    it depend greatly of the game you are doing.

    a simple solution:
    save your value in player prefs
    when you enter a scene you have a PlayerData script that you observe for changes. so when you finish loading your saved values, all other player will receive your changes.
     
    gk104 likes this.
  3. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    But what if i want to save the data on a server? on MySQL database?
     
  4. Tiny-Tree

    Tiny-Tree

    Joined:
    Dec 26, 2012
    Posts:
    1,315
    it still not depend of unity networking
     
  5. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    You could also have a conatiner object with all the information you need, which won't be destroyed when switching scenes. Just make a gameobject with the information about the player, and then add this script to the playerObject:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DontDestroy : MonoBehaviour {
    5.  
    6.     void Start () {
    7.         DontDestroyOnLoad(gameObject);
    8.     }
    9. }
    10.  
    (You don't have to make a new "DontDestory.cs" script if you just need it once, then just add the command "DontDestroyOnLoad(gameObject);" to any startMethod of your container object.

    Saving will be important as well, but not necessary for your current task.