【发布时间】:2018-12-15 00:42:40
【问题描述】:
我目前正在使用 c# 开发一款游戏,但出现了一个让我陷入困境的问题。问题是,我不知道如何才能打印出字符串“sprite”的分配值。我正在开发的游戏将为 sprite 字符串分配多个值,这就是为什么我不只是在声明字符串时为其分配一个值。
为简单起见,我将场景简化为更简单的示例。
class Program
{
static void Main(string[] args)
{
Player p;
p = new Player();
Console.Read();
}
}
class GameObject
{
public GameObject(string spriteName)
{
Console.WriteLine(spriteName);
}
}
class Player : GameObject
{
public static string sprite;
public Player() : base(sprite)
{
sprite = "Test";
}
}
}
【问题讨论】:
-
请注意,使用
new Player完全违背了 Unity 的思维方式,应该永远这样做。你不newGameObjects -
恕我直言,您应该
public static string sprite = "Test"。您正在查看的行为是基于构造函数的执行顺序。 -
“为简单起见,我将场景简化为一个更简单的示例”这正是说明问题的最佳方式。让它尽可能简单。
标签: c#