0

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.

"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 all UnityEngine.MonoBehaviour:.ctor()"

What is the proper way to instantiate a number of instances of my particle class (each containing a singular sphere GameObject)?

Particle Class:

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 Class:

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

    }
}

Any help is greatly appreciated!

Marcus Koz
  • 255
  • 5
  • 21

1 Answers1

1

Your code looks fine other than you may have accidentally typed the declaration wrong for gameObj

Change GameObject gameObj = new GameObject(); to just GameObject gameObj = null; in your Particle class.

The error specifically mentions that you cannot do what you did and in your Start() you are setting it like it mentioned.

EDIT: 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 is defined on MonoBehaviour so you should have access to it already.

TyCobb
  • 8,909
  • 1
  • 33
  • 53
  • If I understand you correctly, you are saying that the only change that should be made is the initialization of the GameObject to null within the Particle class. However, I am still receiving the warning regarding not using the 'new' keyword within CustomParticleSystem's Start method. The code runs, but no particle is visible at the hardcoded initial position in the scene. – Marcus Koz Oct 06 '14 at 18:10
  • 1
    @MattKoczwara Ah. I see. Your `Particle` is a `MonoBehaviour`. You need to get an instance of `GameObject` and then set your particles to `gameObject.AddComponent()` in `CustomParticleSystem ` – TyCobb Oct 06 '14 at 18:15
  • Thank you! That solved the problem. Would you happen to know how to then access the properties/methods for each particle/gameObject? – Marcus Koz Oct 06 '14 at 18:24
  • @MattKoczwara Not sure exactly what you are referring to. If you do `Particle p = gameObject.AddComponent();` you should be able to have full access to all properties on `p`. – TyCobb Oct 06 '14 at 18:34
  • I see that the properties are available once a particle is set to the gameObject. Thanks again. – Marcus Koz Oct 06 '14 at 18:35