问题描述
我正在尝试在Unity中使用C#脚本实例化大量粒子".我创建了一个粒子类,其中包含相应GameObject的创建.每个粒子实例中的GameObject是一个球体.尝试实例化新粒子(粒子p = new粒子(...))时,我收到Unity警告,不应使用'new'关键字.
I am attempting to instantiate a large number of "particles" using a C# script in Unity. I have created a particle class that contains the creation of a corresponding GameObject. The GameObject within each particle instance is a sphere. When attempting to instantiate a new particle (Particle p = new Particle(...)) I get a Unity warning that the 'new' keyword should not be used.
您正尝试使用'new'关键字创建MonoBehaviour.不允许这样做.只能使用AddComponent()添加MonoBehaviours.或者,您的脚本可以继承自ScriptableObject或根本不继承基类UnityEngine.MonoBehaviour:.ctor()"
"You are trying to create a MonoBehaviour 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 allUnityEngine.MonoBehaviour:.ctor()"
实例化我的粒子类的多个实例的正确方法是什么(每个实例都包含一个奇异的球GameObject)?
What is the proper way to instantiate a number of instances of my particle class (each containing a singular sphere GameObject)?
粒子类:
public class Particle : MonoBehaviour {
Vector3 position = new Vector3();
Vector3 velocity = new Vector3();
Vector3 force = new Vector3();
Vector3 gravity = new Vector3(0,-9.81f,0);
int age;
int maxAge;
int mass;
GameObject gameObj = new GameObject();
public Particle(Vector3 position, Vector3 velocity)
{
this.position = position;
this.velocity = velocity;
this.force = Vector3.zero;
age = 0;
maxAge = 250;
}
// Use this for initialization
void Start () {
gameObj = GameObject.CreatePrimitive (PrimitiveType.Sphere);
//gameObj.transform.localScale (1, 1, 1);
gameObj.transform.position = position;
}
// FixedUopdate is called at a fixed rate - 50fps
void FixedUpdate () {
}
// Update is called once per frame
public void Update () {
velocity += gravity * Time.deltaTime;
//transform.position += velocity * Time.deltaTime;
gameObj.transform.position = velocity * Time.deltaTime;
Debug.Log ("Velocity: " + velocity);
//this.position = this.position + (this.velocity * Time.deltaTime);
//gameObj.transform.position
}
}
CustomParticleSystem类:
public class CustomParticleSystem : MonoBehaviour {
Vector3 initPos = new Vector3(0, 15, 0);
Vector3 initVel = Vector3.zero;
private Particle p;
ArrayList Particles = new ArrayList();
// Use this for initialization
void Start () {
Particle p = new Particle (initPos, initVel);
Particles.Add (p);
}
// Update is called once per frame
void Update () {
}
}
任何帮助将不胜感激!
推荐答案
您的代码看起来不错,除了您可能不小心为gameObj
Your code looks fine other than you may have accidentally typed the declaration wrong for gameObj
在Particle
类中将GameObject gameObj = new GameObject();
更改为GameObject gameObj = null;
.
该错误专门提到您无法执行操作,并且在您的Start()
中按所提到的进行设置.
The error specifically mentions that you cannot do what you did and in your Start()
you are setting it like it mentioned.
查看Particle
,它继承了MonoBehaviour
.您需要gameObject
使用gameObject.AddComponent<Particle>();
Looking at Particle
, it inherits MonoBehaviour
. You need to have gameObject
create the instance for you using gameObject.AddComponent<Particle>();
http://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html
gameObject
是在MonoBehaviour
上定义的,因此您应该已经可以访问它.
gameObject
is defined on MonoBehaviour
so you should have access to it already.
这篇关于如何实例化包含GameObject的对象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!