简体   繁体   中英

Error CS0246: The type or namespace name 'StreamingContext' could not be found (are you missing a using directive or an assembly reference?)

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?)

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.

this is the code y was making the error is in the line 7

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private new RigidBody rigidbody;

    public float movementSpeed;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody = GetComponent<RigidBody>();
    }

    // Update is called once per frame
    void Update()
    {
        float hor = Input.GetAxisRaw("Horizontal");
        float ver = Input.GetAxisRaw("Vertical");

        if (hor != 0 || ver != 0) {
            Vector3 direction = (transform.forward * ver + transform.right * hor).normalized;

            rigidbody.velocity = direction * movementSpeed;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) error CS0246: The type or namespace name 'BannerPosition' could not be found are you missing a using directive or an assembly reference? error CS0246: The type or namespace name 'Npgsql' could not be found (are you missing a using directive or an assembly reference?) Error CS0246 The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) Error CS0246 The type or namespace name 'CreateRandomAnswersForKey' could not be found (are you missing a using directive or an assembly reference?)? CS0246: The type or namespace name 'Employee' could not be found (are you missing a using directive or an assembly reference?)? CS0246 The type or namespace name 'ErrorViewModel' could not be found (are you missing a using directive or an assembly reference?) CS0246: The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference Unity error CS0246: The type or namespace name 'InputField' could not be found (are you missing a using directive or an assembly reference?) C# Error: Error CS0246 The type or namespace name '' could not be found (are you missing a using directive or an assembly reference?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM