0

I have a project of cabling in unity and I want to save all the modification that a player does in the scene.

How should i save the game? How can I save my gameobject? I need to serialize my gameobject but I don't know how to do this!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class GameController : MonoBehaviour 
{    
    public Transform player;

    int i;

    void Start ()
    {
        i = 1; 
    }

    void Update () 
    {
        if (Input.GetKeyUp(KeyCode.Escape))     
        {   
            Pause(); 
        }

    if(player.GetComponent<FirstPersonController>().enabled == false) 
    {
        if (Input.GetKey (KeyCode.UpArrow))
        { 
            Debug.Log ("Up pressed");
            player.transform.position = new Vector3(player.transform.position.x,player.transform.position.y + 1.0f,player.transform.position.z); 
        } 
        else if (Input.GetKey (KeyCode.DownArrow))
        { 
            Debug.Log ("Down pressed");
            player.transform.position = new Vector3(player.transform.position.x,player.transform.position.y - 1.0f,player.transform.position.z); 
        }
    }
}
Alex Myers
  • 6,196
  • 7
  • 23
  • 39
  • Possible duplicate of [Serialize and Deserialize Json and Json Array in Unity](https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity) – Draco18s no longer trusts SE May 13 '19 at 16:51

1 Answers1

0

So, I assume that - in another script - you are going to load a new Scene, and you want to maintain the object having the GameController script attached to it.

In this case, you can use the DontDestroyOnLoad() call, the trivial form goes like this (just add the Awake() function to your class):

void Awake()
{
    DontDestroyOnLoad(this.gameObject);
}

This way, the object won't be destroyed when a new scene is loaded.

In case you want to save data when the game is closed, in order to load them afterwards, PlayerPrefs are the answer.

Andrea
  • 6,032
  • 2
  • 28
  • 55
  • there is no other way for saving gameobjects ? – Fatima May 13 '19 at 15:28
  • These ones are the solutions that Unity provides out-of-the-box. Custom solutions can be created (like using a database, or an XML), but obviously they are more complex because you have to use your own code. – Andrea May 13 '19 at 15:30
  • because i used DontDestroyOnLoad() but it didnt work.when i exited from the game ,everythings came like initial scene. i want to save my game – Fatima May 13 '19 at 15:44
  • There's no such method in your question. Adding it could help us addressing the exact problem. – Andrea May 13 '19 at 15:53