FANDOM


Containers are GameObjects that store a collection of inventory items that the player can retrieve. They are typically a per-scene object, but this script allows you to create a Container whose contents are the same in all scenes.

To use it:

  1. Create a new C# script named SyncContainers.cs, and paste in the contents below
  2. Drop a new Container in the scene and add the new Sync Containers component
  3. Check Retain in prefab? in the Remember Container Inspector and make a new prefab from it
  4. Place an instance of this prefab in each scene.

SyncContainers.cs:

using UnityEngine;
using System.Collections;
using AC;

[RequireComponent (typeof(RememberContainer))]
public class SyncContainers : MonoBehaviour
{

private int constantID = 0;
private RememberContainer rememberContainer;


private void OnEnable ()
{
rememberContainer = GetComponent <RememberContainer>();
constantID = rememberContainer.constantID;

EventManager.OnAfterChangeScene += OnAfterChangeScene;
}


private void OnDisable ()
{
EventManager.OnAfterChangeScene -= OnAfterChangeScene;
}


private void OnAfterChangeScene (LoadingGame loadingGame)
{
if (constantID == 0)
{
return;
}

if (loadingGame == LoadingGame.No || loadingGame == LoadingGame.JustSwitchingPlayer)
{
SceneInfo previousSceneInfo = KickStarter.sceneChanger.GetPreviousSceneInfo (false);
Debug.Log ("Load data for scene " + previousSceneInfo.name + " - " + previousSceneInfo.number + ", Searching for data ID: " + constantID);

foreach (SingleLevelData levelData in KickStarter.levelStorage.allLevelData)
{
if (levelData.sceneNumber == previousSceneInfo.number)
{
foreach (ScriptData _scriptData in levelData.allScriptData)
{
if (_scriptData.data != null && _scriptData.data.Length > 0 && _scriptData.objectID == constantID)
{
Debug.Log ("Found save data in last scene, applying to self.", gameObject);
rememberContainer.LoadData (_scriptData.data);
}
}
}
}
}
}

}
Community content is available under CC-BY-SA unless otherwise noted.