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

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

问题描述

团结并不喜欢这个,而是经过研究和来自团结的警告

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-关键字

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

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天全站免登陆