- Home /
GetComponent help
I have this script:
function OnTriggerEnter(){
gameObject.GetComponent("Ellipsoid Particle Emitter").enabled = true;
}
But when i shoot at it to turn on the particle emitter for an explosion. I get the error NullReference Exception at line 3.
How can i fix this or is their an easier way to achieve this goal?
Thanks!!
Answer by tingham · Feb 06, 2011 at 07:47 PM
I think you might want:
C#
gameObject.GetComponent<ParticleEmitter>().enabled = true;
Javascript
GetComponent.<ParticleEmitter>();
Try that.
$$anonymous$$uch better code, but gameObject isn't necessary. And in JavaScript, you need a dot. GetComponent.()
Heh, between Objective-C, C#, Javascript and Perl I get confused sometimes. $$anonymous$$y bad.
Answer by SarperS · Feb 06, 2011 at 07:40 PM
Did you set the particle system to auto destruct? If so, the best way to do it is to create a prefab out of that particle system and use the below code.
var explosionEffect : GameObject; //Set it to your prefab in inspector
function OnTriggerEnter(){ Instantiate(explosionEffect, transform.position, Quaternion.identity); }
Your answer
Follow this Question
Related Questions
NullReferenceException-Error when trying to GetComponent 3 Answers
NullReferenceException. GetComponent dose not work properly 2 Answers
GetComponent usage returns NullReferenceException, not sure why 1 Answer
NullReferenceException: Object reference not set to an instance of an object ..... 1 Answer