Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 变量值未在字符串中更新_C#_Unity3d - Fatal编程技术网

C# 变量值未在字符串中更新

C# 变量值未在字符串中更新,c#,unity3d,C#,Unity3d,我有一个名为“DialogueLines.cs”的类,其中有一个公共静态字符串列表。问题是,当我访问此特定字符串时: public static volatile string cutscene_introHurt7 = "* " + Manager.playerName + " huh?\n That's a nice name."; Manager.playerName的值不正确。开始时,playerName的值设置为“Garrett”。当更新到其他内容时,如“Zip”,对话仍然会说:

我有一个名为“DialogueLines.cs”的类,其中有一个公共静态字符串列表。问题是,当我访问此特定字符串时:

public static volatile string cutscene_introHurt7 = "* " + Manager.playerName + " huh?\n  That's a nice name.";
Manager.playerName
的值不正确。开始时,playerName的值设置为“Garrett”。当更新到其他内容时,如“Zip”,对话仍然会说:
加勒特,嗯?这是一个很好的名称。我还检查了Debug.Log()语句,以确保名称正确更改。我假设这是因为字符串没有用正确的变量值更新。如您所见,我已经尝试将volatile关键字粘贴到字符串上,但没有成功。有什么想法吗?谢谢。

这是由于
静态的行为造成的。静态将预编译字符串,这意味着即使更改用户名,预编译的字符串也不会更改

但是,只需更改字符串即可。在你使用之前把整个作业再做一遍

cutscene_introHurt7 = "* " + Manager.playerName + " huh?\n  That's a nice name.";

但是,如果可能的话,你可能想考虑让它变成非静态的。之后,你的预期行为就会起作用

下面是一个示例控制台应用程序,以查看正在运行的静态解决方案

using System;

class Program
{
    public static string playerName = "GARRET";
    // This will be concatonated to 1 string on runtime "* GARRET huh? \m That's a nice name."
    public static volatile string cutscene_introHurt7 = "* " + playerName + " huh?\n  That's a nice name.";

    static void Main(string[] args)
    {
        // We write the intended string
        Console.WriteLine(cutscene_introHurt7);
        // We change the name, but the string is still compiled
        playerName = "Hello world!";
        // Will give the same result as before
        Console.WriteLine(cutscene_introHurt7);
        // Now we overwrite the whole static variable
        cutscene_introHurt7 = "* " + playerName + " huh?\n  That's a nice name.";
        // And you do have the expected result
        Console.WriteLine(cutscene_introHurt7);
        Console.ReadLine();
    }
}

在IEnumerator中为Manager.playerName?设置值的位置。该值更新良好,如Debug.Log语句所示,该值和其他正在更新的文本字段也很好。这只是一个字符串,不知何故,它没有正确的值。可能是因为它是静态的,或者我怎样才能强制刷新或其他什么?很有趣,谢谢。不幸的是,没有更简单的方法来强制刷新,但哦,好吧。