- Home /
Very simple component-access question
I create a cube. I add a particle emitter component. (I click on Component/Effects/Particle System) I change nothing else. I add this script to the cube:
function Start () {
gameObject.particleEmitter.emit = false;
}
It should instantly turn off the emitter. Seems simple, but when I run this, I get the following error:
MissingComponentException: There is no 'ParticleEmitter' attached to the "TestBox" game object, but a script is trying to access it.
I don't understand, because there is clearly a particle emitter component attached to this cube. Why is Unity claiming there isn't?
EDIT:
I forgot there is the new particle system, so I changed it to particleSystem instead:
function Start () {
gameObject.particleSystem.emit = false;
}
But it just gives me the error:
'ParticleSystem' is not a member of 'UnityEngine.GameObject'. Did you mean 'particleEmitter'?
What what what?!?!!
Yes, I forgot about GetComponent, thank you. I tried this:
gameObject.GetComponent(ParticleEmitter).emit = false;
But I just got this error:
NullReferenceException: Object reference not set to an instance of an object
I cannot believe how hard a time I am having with this. I've accomplished a lot in Unity so far, but I've been trying to figure out this simple problem for literally weeks. It HAS to be simple! What am I doing wrong?
EDIT:
Here is a screenshot of the whole setup:
Ahha!
gameObject.GetComponent(ParticleSystem).enableEmission = false;
This is it! This is the code that does it! Thank you so much for your help, but I've finally solved this! Oh my God that was embarrassingly difficult!
also
http://docs.unity3d.com/Documentation/ScriptReference/ParticleSystem.html
"variables' section ..
Thank you. That was the page that helped me find the answer.
Why did you delete your previous comments? I'm just curious.
hooray!
Good question ... I always delete comments (and questions) that become irrelevant. This site has way, way too much rubbish on it.
Since you completely solved your problem, and you stated the solution (for anyone in the future), I just deleted my cruft.
Rock On !!!