c# - How to instantiate object class that contains GameObject? -
c# - How to instantiate object class that contains GameObject? -
i attempting instantiate big number of "particles" using c# script in unity. have created particle class contains creation of corresponding gameobject. gameobject within each particle instance sphere. when attempting instantiate new particle (particle p = new particle(...)) unity warning 'new' keyword should not used.
"you trying create monobehaviour using 'new' keyword. not allowed. monobehaviours can added using addcomponent(). alternatively, script can inherit scriptableobject or no base of operations class @ unityengine.monobehaviour:.ctor()"
what proper way instantiate number of instances of particle class (each containing singular sphere gameobject)?
particle class:
public class particle : monobehaviour { vector3 position = new vector3(); vector3 velocity = new vector3(); vector3 forcefulness = 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; } // utilize initialization void start () { gameobj = gameobject.createprimitive (primitivetype.sphere); //gameobj.transform.localscale (1, 1, 1); gameobj.transform.position = position; } // fixeduopdate called @ fixed rate - 50fps void fixedupdate () { } // update called 1 time 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(); // utilize initialization void start () { particle p = new particle (initpos, initvel); particles.add (p); } // update called 1 time per frame void update () { } }
any help appreciated!
your code looks fine other may have accidentally typed declaration wrong gameobj
change gameobject gameobj = new gameobject();
gameobject gameobj = null;
in particle
class.
the error mentions cannot did , in start()
setting mentioned.
edit: looking @ particle
, inherits monobehaviour
. need have gameobject
create instance using gameobject.addcomponent<particle>();
http://docs.unity3d.com/scriptreference/gameobject.addcomponent.html
gameobject
defined on monobehaviour
should have access already.
c# unity3d instantiation particles gameobject
Comments
Post a Comment