首页 > 解决方案 > 当 streamwriter 语句使用 void 和 int 时,如何解决布尔错误?

问题描述

所以我有这个代码:

StreamWriter sw;
sw = new StreamWriter(Application.dataPath + "safedata.txt");
sw.WriteLine(MainMenuscreen.ToggleShadows(MainMenuscreen.tc)); // This line is givng me the error

这是显示的错误代码:

严重性代码 描述 项目文件行抑制状态 抑制状态错误 CS1503 参数 1:无法从 'void' 转换为 'bool' Assembly-CSharp C:\Users\Owner\Downloads\Project 3 (1)\Project 3\Assets\Scripts\ SaveData.cs 46 活动

我不明白为什么会弹出这个错误,因为 ToggleShadows 是 void 函数,而 tc 是一个 int 变量:

public static int tc;

public static void ToggleShadows(int newToggle)
{
    Light[] lights = GameObject.FindObjectsOfType<Light>();

    foreach (Light light in lights)
    {
        if (newToggle == 0)
            light.shadows = LightShadows.None;
        else if (newToggle == 1)
            light.shadows = LightShadows.Hard;
        else
            light.shadows = LightShadows.Soft;
    }
    return;
}

我不知道为什么会出现布尔值。我正在尝试将数据从我的 gameSetting 文件中获取到我的 savedata 文件中。到目前为止,这个函数和变量只是给我问题的语句。

这是两个完整的代码:

//Hakeem Thomas
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;


public class SaveData : MonoBehaviour
{
    public GameObject player;
    public Inventory inv;
    //public MainMenuscreen gsettings;

    StreamWriter sw;
    StreamReader sr;
    void Awake()
    {
        Load();
    }

    //void Update()
    //{
    //    if(Input.GetKeyDown(KeyCode.K))
    //    { Save(); }
    //    if (Input.GetKeyDown(KeyCode.O))
    //    { Load(); }
    //}

    public void Save()
    {
        sw = new StreamWriter(Application.dataPath + "safedata.txt");
        sw.WriteLine(player.transform.position.x);
        sw.WriteLine(player.transform.position.y);
        sw.WriteLine(player.transform.position.z);

        foreach (Item ii in Inventory.invItems)
        {
            sw.WriteLine(ii);
        }

        sw.WriteLine(MainMenuscreen.hSliderValue);
        sw.WriteLine(MainMenuscreen.hPitch);
        sw.WriteLine(MainMenuscreen.hStereoPan);
        sw.WriteLine(MainMenuscreen.hfov);
        sw.WriteLine(MainMenuscreen.ToggleShadows(MainMenuscreen.tc));
        sw.Close();
    }

    public void Load()
    {
        try
        {
            sr = new StreamReader(Application.dataPath + "safedata.txt");
            float xpos = float.Parse(sr.ReadLine());
            float ypos = float.Parse(sr.ReadLine());
            float zpos = float.Parse(sr.ReadLine());
            player.transform.position = new Vector3(xpos, ypos, zpos);
            while (!sr.EndOfStream)
            {
                inv.AddToInventory((Item)Convert.ChangeType(sr.ReadLine(), typeof(Item)));

            }


            sr.Close();

        }
        catch
        {
            player.transform.position = new Vector3(-16.9f, 0.9f, 11f);
        }
    }
}

//Hakeem Thomas
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MainMenuscreen : MonoBehaviour
{
    public static float hSliderValue = 0.5f;
    public static float hPitch = 1.0f;
    public static float hStereoPan = 0.0f;
    public static float hfov = 60.0f;
    public static int tc;
    public Camera vid;
    public SaveData op;
    public static bool sound_options = false;

    SaveData sd;
    GUI_2D m;
    // Start is called before the first frame update
    void Start()
    {
        tc = 3;
    }

    public static void ToggleShadows(int newToggle)
    {
        Light[] lights = GameObject.FindObjectsOfType<Light>();

        foreach (Light light in lights)
        {
            if (newToggle == 0)
                light.shadows = LightShadows.None;
            else if (newToggle == 1)
                light.shadows = LightShadows.Hard;
            else
                light.shadows = LightShadows.Soft;
        }
        return;
    }

    public void SetFOV(float newFOV)
    {
        Camera.main.fieldOfView = newFOV;
    }

    void OnGUI()
    {
        void GSettings()
        {
            if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("MainMenu"))
            {
                if (GUI.Button(new Rect(600, 400, 100, 50), "Game Settings"))
                {
                    sound_options = true;
                }
            }
            if (Input.GetKeyDown("escape"))
            {
                if (sound_options != true)
                {
                    sound_options = true;
                }
            }
            else if (Input.GetKeyDown("escape"))
            {
                if (sound_options == true)
                    sound_options = false;
            }
        }

        if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("MainMenu"))
        {
            if (!sound_options)
            {
                if (GUI.Button(new Rect(600, 200, 100, 50), "New Game"))
                {
                    SceneManager.LoadScene("SampleScene");
                }
                if (GUI.Button(new Rect(600, 300, 100, 50), "Resume Game"))
                {
                    SceneManager.LoadScene("SampleScene");
                    sd.Load();
                }

                GSettings();
            }
        }
        else
        {
            GSettings();
        }

        if (sound_options)
        {
            if (GUI.Button(new Rect(600, 300, 100, 50), "Save Game"))
            {
                op.Save();
            }
            GUI.Label(new Rect(550, 25, 100, 30), "Volume");
            hSliderValue = GUI.HorizontalSlider(new Rect(600, 25, 100, 30), hSliderValue, 0.0F, 1.0F);
            GUI.Label(new Rect(550, 50, 100, 30), "Pitch");
            hPitch = GUI.HorizontalSlider(new Rect(600, 50, 100, 30), hPitch, -3.0F, 3.0F);
            GUI.Label(new Rect(500, 75, 100, 30), "StereoPan");
            hStereoPan = GUI.HorizontalSlider(new Rect(600, 75, 100, 30), hStereoPan, 0.0F, 1.0F);
            if (GUI.Button(new Rect(600, 125, 75, 20), "High"))
            { Screen.SetResolution(0, 3, true); }   //GetComponent<Video_Config>().SetResolution(0, 3);
            GUI.Label(new Rect(500, 100, 100, 30), "Field of View");
            hfov = GUI.HorizontalSlider(new Rect(600, 100, 100, 30), hfov, 60.0f, 120.0f);
            if (GUI.Button(new Rect(600, 150, 75, 20), "Shadows"))
            {
                if (tc == 0)
                {
                    ToggleShadows(tc);
                    tc = 1;
                }
                else if (tc == 1)
                {
                    ToggleShadows(tc);
                    tc = 2;
                }
                else
                {
                    ToggleShadows(tc);
                    tc = 0;
                }
            }
        }
        //GUI.Label(new Rect(25, 125, 100, 30), "FullScreen");
        //full = GUI.Toggle(new Rect(95, 125, 100, 30), fullscreen, " On/ Off");
    }

    // Update is called once per frame
    void Update()
    {
        if (vid.fieldOfView != hfov)
        { vid.fieldOfView = hfov; }


    }
}

标签: c#.netunity3d

解决方案


@HimBromBeere 用以下内容回答了我的问题:

sw.WriteLine(MainScreenMenu.tc)

推荐阅读