Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.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,我目前正在开发PacMan作为一个学校项目。我有一个名为ghost的基类,它可以找到从ghost到目标位置的最短路径。我也有继承它的类,比如redghost,这些类将给基类一个目标和ghost的当前位置。当我使用红色幽灵时,它会工作。。。这些变量在基类中被更改,然后就可以使用了,但是对于任何其他重影来说,它都不起作用,我似乎不明白为什么。感谢您的帮助:) 正如我所说的,红色幽灵设定变量和追逐模式工作。。。但是粉红幽灵并不是因为某些原因吗?我所看到的唯一区别是,PinkHost不是公共的。不,不幸

我目前正在开发PacMan作为一个学校项目。我有一个名为ghost的基类,它可以找到从ghost到目标位置的最短路径。我也有继承它的类,比如redghost,这些类将给基类一个目标和ghost的当前位置。当我使用红色幽灵时,它会工作。。。这些变量在基类中被更改,然后就可以使用了,但是对于任何其他重影来说,它都不起作用,我似乎不明白为什么。感谢您的帮助:)


正如我所说的,红色幽灵设定变量和追逐模式工作。。。但是粉红幽灵并不是因为某些原因吗?

我所看到的唯一区别是,
PinkHost
不是公共的。

不,不幸的是,这不起作用:(所有变量仍然显示为0,而不是它们的实际值。但是PinkHost有另一个游戏对象,而不是redghost(pinky vs blinky)-这可能会影响变量。我最近在我的帖子中添加了一条关于这一点的新注释。在set variables类中,它显示了我需要的实际值,而在Ghost类中,这些不传输acrossI,我已经在其中放置了debug.log,它说ghostposx和ghostposy的值分别为21和23,而当我这样做时debug.log在ghosts类中为这两个类输出0的变量相同。这不会解决您的问题,但是:(1)正确地发明代码,将更易于阅读。(2)将单独的类放在单独的文件中。(3)原始的ghosts每个都有不同的寻道行为(对所有四个使用相同的技术将导致它们堆积在玩家后面,玩家可以永远躲避它们。)
它不起作用的确切含义是什么?
    pinkghost = new PinkGhost();
    redghost = new RedGhost();

    pinkghost.SetVariables();
    pinkghost.ChaseMode();
    redghost.SetVariables();
    redghost.ChaseMode();

public abstract class Ghosts
{
    public int ScatterX;
    public int ScatterY;
    public int TargetX;
    public int TargetY;
    public int GhostPosX;
    public int GhostPosY;
    public int StartNodeX;
    public int StartNodeY;
    public float speed = 1.0f;

    abstract public void SetVariables();
    abstract public void ChaseMode();

    public Ghosts()
    {
        //Allows ghost to access pacman coords
        PacMan = GameObject.Find("pacman");
    }


public class RedGhost : Ghosts
{
    public override void SetVariables()
    {
        GhostMov = gameobject.Find("blinky");
        ScatterX = 24;
        ScatterY = 3;
        GhostPosX = 13 + Convert.ToInt32(GhostMov.transform.position.x);
        GhostPosY = 15 - Convert.ToInt32(GhostMov.transform.position.y);
    }
}

// This one works…
class PinkGhost : Ghosts
{
    public override void SetVariables()
    {
        GhostMov = gameobject.Find("pinky");
        ScatterX = 4;
        ScatterY = 3;
        GhostPosX = 13 + Convert.ToInt32(GhostMov.transform.position.x);
        GhostPosY = 15 - Convert.ToInt32(GhostMov.transform.position.y);
    }
}