在抽象类的多个儿童实例中需要一些帮助

发布于 2025-02-13 20:07:49 字数 1178 浏览 0 评论 0原文


Hiya-所以我认为我在这里使用抽象课程有点误会,因此,如果不是太多麻烦,我需要有人向我解释我在这里出错的地方;


因此,我有一个像这样构成的抽象类:

 [RequireComponent(typeof(Rigidbody2D))]
 [RequireComponent(typeof(Collider2D))]
 
 public abstract class Enemy : MonoBehaviour {
     // Physics
     protected Rigidbody2D rb;
     protected Collider2D col;
 
     protected virtual void Awake(){
         // Physics
             rb = this.GetComponent<Rigidbody2D>();
             col = this.GetComponent<Collider2D>();
             col.isTrigger = true;
     }
 }

然后是一个继承它的类:

 public class Whizzer : Enemy {
         protected override void Awake(){
               // Physics
               rb = GetComponent<Rigidbody2D>();
               rb.bodyType = RigidbodyType2D.Dynamic;
               rb.gravityScale = 0f;
 
               col = GetComponent<Collider2D>();
               col.isTrigger = true;
        }
 }

但是,我遇到的问题是,当我在Whizzer类中引用RB或COL时(我认为)访问抽象敌方类别中定义的变量 -自从上次运行的脚本分配其僵化的机体和对撞机作为抽象敌人类中的变量我的问题我的问题是,我如何创建变量的单独实例,这只会导致一个与此脚本一起工作的游戏对象的一个

​​实例。 RB和COL为每个喘息的类实例而创建的,同时保持抽象敌人类中所需的变量? 还是这甚至是正确的方法?

先感谢您!



Hiya - so i think i'm at a bit of a misunderstanding here with using abstract classes so if it isn't too much trouble i need someone to explain to me where i've gone wrong here;


So i have an abstract class structured like this:

 [RequireComponent(typeof(Rigidbody2D))]
 [RequireComponent(typeof(Collider2D))]
 
 public abstract class Enemy : MonoBehaviour {
     // Physics
     protected Rigidbody2D rb;
     protected Collider2D col;
 
     protected virtual void Awake(){
         // Physics
             rb = this.GetComponent<Rigidbody2D>();
             col = this.GetComponent<Collider2D>();
             col.isTrigger = true;
     }
 }

Then a class inheriting it:

 public class Whizzer : Enemy {
         protected override void Awake(){
               // Physics
               rb = GetComponent<Rigidbody2D>();
               rb.bodyType = RigidbodyType2D.Dynamic;
               rb.gravityScale = 0f;
 
               col = GetComponent<Collider2D>();
               col.isTrigger = true;
        }
 }

However the problem i'm having is that when i reference rb or col inside the Whizzer class it's (i think) accessing the variables defined in the abstract Enemy class - this results in only one instance of a gameobject with this script working at a time since the last script to run assigns their rigidbody and collider as the variables in the abstract Enemy class

My question is how can i create seperate instances of the variables rb and col for every Whizzer class instance created whilst keeping the variables desired inside the abstract enemy class?
Or if this is even the right way to go about doing this?

Thankyou in advance!


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃素的狼 2025-02-20 20:07:49

他们应该是腐烂的。如果您希望在所有实例中共享一个变量,则应使用“静态”修饰符。但是您不使用它,因此敌人的每个实例(Whizzer也是敌人)应该对这些领域具有自己的价值。您怎么知道他们指出了对撞机和僵硬的体系相同的实例?

关于您的实现,您略有缺少重点:

public abstract class Enemy : MonoBehaviour {
    protected Rigidbody2D rb;
    protected Collider2D col;

    protected virtual void Awake(){        
         rb = this.GetComponent<Rigidbody2D>();
         col = this.GetComponent<Collider2D>();
         col.isTrigger = true;
    }
}

然后,一类继承它:

public class Whizzer : Enemy {
     protected override void Awake(){
           base.Awake();               
           rb.bodyType = RigidbodyType2D.Dynamic;
           rb.gravityScale = 0f; 
    }
}

恕我直言,您应该在大多数情况下使用基本方法,而不是粘贴相同的代码来覆盖

They should be saparate. If you want a variable to be shared among all instances you should use "static" modifier. But you do not use that so each instance of Enemy (Whizzer is also an Enemy) should have it's own value for these fields. How do you know that they point to the same instances of collider and rigidbody?

About your implementation you are slightly missing the point:

public abstract class Enemy : MonoBehaviour {
    protected Rigidbody2D rb;
    protected Collider2D col;

    protected virtual void Awake(){        
         rb = this.GetComponent<Rigidbody2D>();
         col = this.GetComponent<Collider2D>();
         col.isTrigger = true;
    }
}

Then a class inheriting it:

public class Whizzer : Enemy {
     protected override void Awake(){
           base.Awake();               
           rb.bodyType = RigidbodyType2D.Dynamic;
           rb.gravityScale = 0f; 
    }
}

That makes more sense imho, you should use base method in most cases instead of pasting the same chunk of code to override

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文