ParticleEmitter woes

Im new to unity, so be gentle. I’m still coming to terms with components and scripting a Particle Emitter on a Sphere is doing my head in.

class Moon {
	
  var rock: GameObject;
  var velocity: int; 	
  var rotation: int;	
	
  function Moon() {
		
    this.rock = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    this.rock.AddComponent(typeof(Rigidbody));
    this.rock.AddComponent(typeof(ParticleEmitter));
    this.rock.AddComponent(typeof(ParticleRenderer));
  }
	
}

elsewhere…

var body = new Moon();
body.rock.transform.localPosition = bodyPosition;
body.rock.transform.localScale = bodyScale;
body.rock.rigidbody.useGravity = false;

//
// Try and do stuff with the particle emitter 
//

So i had assumingly added a ParticleEmitter ParticleRenderer to my sphere (moon). But any attempt to manipulate either gives me the following error.

Essentially I want some particles to emit from the sphere. That’s not asking for much.

MissingComponentException: There is no 'ParticleEmitter' attached to the "Sphere" game object, but a script is trying to access it.
You probably need to add a ParticleEmitter to the game object "Sphere". Or your script needs to check if the component is attached before using it.

I thought i would ask the experts.

Thanks in advance!

Hi, welcome to the forum!

Generally, you need to add particle emitter, particle animator and particle renderer components to get a working particle system, and it is usually better to add these from the editor rather than from code. To access any of these components from the script, you can use GetComponent:-

var emitter: ParticleEmitter;

function Start() {
    emitter = GetComponent(ParticleEmitter);
    emitter.Emit(); // Or whatever...
}