在 C# 中声明一个类的新实例 [英] Declaring a new instance of a class in C#

查看:28
本文介绍了在 C# 中声明一个类的新实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Unity 不喜欢这个,经过研究和 Unity 的警告

Unity is not liking this and upon research and a warning from unity

(您正在尝试使用 'new' 关键字创建 MonoBehavior.这是不允许的.只能使用 AddComponent() 添加 MonoBehaviours.或者,您的脚本可以从 ScriptableObject 继承或根本没有基类.)

("You are trying to create a MonoBehavior using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively your script can inherit from ScriptableObject or no base class at all.)

我知道 AddComponent,但我不认为那是我想要的.

I know of AddComponent, but I don't quite think that's what I want.

public Boids boids = new Boids();

Boids 是我的第二堂课.我在教程中看到了这项工作,所以我对为什么这在 Monodevelop 中不起作用感到困惑.

Boids is my 2nd class. I saw this work on a tutorial, so I'm kind of confused on why this doesn't work in Monodevelop.

推荐答案

很容易 google...

Pretty easy to google...

http://forum.unity3d.com/threads/119537-You-are-trying-to-create-a-MonoBehaviour-using-the-new-keyword

这与 Boids 继承的内容有关.New 显然不是实例化对象的正确方法,它应该使用统一框架进行实例化

It's to do with what Boids inherits. New is apparently not the correct way to instantiate the object, it should be instatiated using the unity framework

gameObject.AddComponent<Boids>()

这篇关于在 C# 中声明一个类的新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆