I have these errors when trying to write this script for save and load in my game.
Assets\Scripts\Save System\SaveData.cs(62,40): error CS0246: The type or namespace name 'StreamingContext' could not be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\Save System\SaveData.cs(13,31): error CS0246: The type or namespace name 'PlaceableObjectData' could not be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\Save System\SaveData.cs(61,6): error CS0246: The type or namespace name 'OnDeserializedAttribute' could not be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\Save System\SaveData.cs(61,6): error CS0246: The type or namespace name 'OnDeserialized' could not be found (are you missing a using directive or an assembly reference?)
That is the script i'm working on:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization;
[Serializable]
public class SaveData
{
public static int IdCount;
public Dictionary<string, PlaceableObjectsData> placeableObjectDatas =
new Dictionary<string, PlaceableObjectsData>();
public static string GenerateId()
{
IdCount ;
return IdCount.ToString();
}
public void AddData(Data data)
{
if (data is placeableObjectDatas plObjData)
{
if (placeableObjectDatas.ContainsKey(plObjData.ID))
{
placeableObjectDatas[plObjData.ID] = plObjData;
}
else
{
placeableObjectDatas.Add(plObjData.ID, plObjData);
}
}
}
public void RemoveData(Data data)
{
if (data is placeableObjectDatas plObjData)
{
if (placeableObjectDatas.ContainsKey(plObjData.ID))
{
placeableObjectDatas.Remove(plObjData.ID);
}
}
}
[OnDeserialized]
internal void OnDeserializedMethod(StreamingContext context)
{
placeableObjectDatas ??= new Dictionary<string, PlaceableObjectsData>();
}
}
EDIT:
This is the script for PlaceableObjectData:
using System;
using UnityEngine;
public class PlaceableObjectsData : Data
{
public string assetName;
public Vector3 position;
}
EDIT 2
Assets\Scripts\Save System\SaveSystem.cs(17,13): error CS0103: The name 'Directory' does not exist in the current context
Assets\Scripts\Save System\SaveSystem.cs(20,13): error CS0103: The name 'Directory' does not exist in the current context
Assets\Scripts\Save System\SaveData.cs(26,21): error CS0246: The type or namespace name 'placeableObjectDatas' could not be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\Save System\SaveSystem.cs(31,28): error CS0246: The type or namespace name 'JsonSerializerSettings' could not be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\Save System\SaveSystem.cs(32,42): error CS0103: The name 'ReferenceLoopHandling' does not exist in the current context
Assets\Scripts\Save System\SaveSystem.cs(34,29): error CS0103: The name 'JsonConvert' does not exist in the current context
Assets\Scripts\Save System\SaveSystem.cs(36,19): error CS1061: 'string' does not contain a definition for 'WriteAllText' and no accessible extension method 'WriteAllText' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\Save System\SaveData.cs(48,21): error CS0246: The type or namespace name 'placeableObjectDatas' could not be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\Save System\SaveSystem.cs(42,22): error CS1061: 'string' does not contain a definition for 'Exists' and no accessible extension method 'Exists' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\Save System\SaveSystem.cs(45,42): error CS1061: 'string' does not contain a definition for 'ReadAllText' and no accessible extension method 'ReadAllText' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\Save System\SaveSystem.cs(47,13): error CS0246: The type or namespace name 'saveData' could not be found (are you missing a using directive or an assembly reference?)
Assets\Scripts\Save System\SaveSystem.cs(47,31): error CS0103: The name 'JsonConvert' does not exist in the current context
Assets\Scripts\Save System\SaveSystem.cs(47,61): error CS0246: The type or namespace name 'saveData' could not be found (are you missing a using directive or an assembly reference?)
CodePudding user response:
You can fix error 1,3 and 4 by adding using System.Runtime.Serialization;
at the top of File.
But I've never heard of PlaceableObjectData, is it maybe a custom class you have created in a other namespace ? Then you have to also import that namespace with the using
keyword.