【问题标题】:How to instantiate object class that contains GameObject? [duplicate]如何实例化包含 GameObject 的对象类? [复制]
【发布时间】:2014-10-06 17:51:44
【问题描述】:

我正在尝试在 Unity 中使用 C# 脚本实例化大量“粒子”。我创建了一个粒子类,其中包含相应 GameObject 的创建。每个粒子实例中的 GameObject 都是一个球体。当尝试实例化一个新粒子(粒子 p = new Particle(...))时,我收到一个 Unity 警告,提示不应使用“新”关键字。

“您正在尝试使用 'new' 关键字创建 MonoBehaviour。这是不允许的。MonoBehaviour 只能使用 AddComponent() 添加。或者,您的脚本可以继承自 ScriptableObject 或根本不继承基类 UnityEngine.MonoBehaviour:.ctor()"

实例化我的粒子类的多个实例(每个实例都包含一个单一的球体游戏对象)的正确方法是什么?

粒子类:

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 () {

    }
}

非常感谢任何帮助!

【问题讨论】:

    标签: c# unity3d instantiation particles gameobject


    【解决方案1】:

    您的代码看起来不错,但您可能不小心为gameObj 输入了错误的声明

    Particle 类中将 GameObject gameObj = new GameObject(); 更改为 GameObject gameObj = null;

    该错误特别提到您无法执行您所做的事情,并且在您的 Start() 中您正在按照提到的方式设置它。

    编辑:查看Particle,它继承了MonoBehaviour。您需要让gameObject 使用gameObject.AddComponent<Particle>(); 为您创建实例

    http://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html

    gameObject 是在 MonoBehaviour 上定义的,因此您应该已经可以访问它了。

    【讨论】:

    • 如果我理解正确的话,你是说唯一应该做的改变是在 Particle 类中将 GameObject 初始化为 null。但是,我仍然收到有关未在 CustomParticleSystem 的 Start 方法中使用“new”关键字的警告。代码运行,但在场景中硬编码的初始位置处看不到任何粒子。
    • @MattKoczwara 啊。我知道了。您的ParticleMonoBehaviour。您需要获取GameObject 的实例,然后在CustomParticleSystem 中将粒子设置为gameObject.AddComponent<Particle>()
    • 谢谢!这解决了问题。你会碰巧知道如何访问每个粒子/游戏对象的属性/方法吗?
    • @MattKoczwara 不确定您指的是什么。如果您使用Particle p = gameObject.AddComponent<Particle>();,您应该能够完全访问p 上的所有属性。
    • 我看到一旦将粒子设置为游戏对象,这些属性就可用。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2018-11-05
    相关资源
    最近更新 更多