- Home /
Compilor Error - Adding a new ParticleEmitter to the object
Hi everyone can someone explain to me why this does not compile.
private function generateNewParticles(chosenComponent:ParticleEmitter)
{
Destroy(selectedEntityObject.GetComponent(ParticleEmitter));
selectedEntityObject.AddComponent(chosenComponent);
}
It says that " No appropriate version of the AddComponent for argument list with ParticleEmitter". I dont understand why this doesnt work because ParticleEmitter is a component. :S
Furthermore chosenComponent is an variable passed through as a component type. However if i were to do this:
print(typeOf(chosenComponent));
It would output "ParticleEmitter" in the console.
Thanks in advance
I think its because you are trying to add a particle emitter as a component. First add the ParticleEmitter component, you can try using the chosenComponent's Type for the AddComponent function. At this point, you could really just say AddComponent(ParticleEmitter). Once the component is there, you can say
selectedEntity.GetComponent(ParticleEmitter).particleEmitter=chosenComponent;
You are passing 'a' particle emitter, not the system type.
Isnt a ParticleEmitter a component? i checked the API and it inherits from Component Class. :s Sorry im afraid i dont fully understand your explanation. Can i not destroy the original particle emitter on the object, then add a new one using the AddComponent function? Also if i just insert ParticleEmitter in the param of AddComponant, how will it know what that points to since its only a type,and also not identifying a variable , in this case chosenComponent..
ahhhhh i seee what you did there, i didnt realise that particle emitter was actually a field in ParticleEmitter Script.
if you want to pass a Type, i think it would have to be type System(i may be wrong here). Think about it this way.
If you overload that function with a different component type, say a String, then it already breaks the overload since it expects a ParticleEmitter. So if you only want to pass the component type, this would not be the way to go about it. What you are passing in the overload would be a specific particle emitter, the type is already declared.
Currently you can pass a chosen particle emitter from somewhere else into your function. You dont need to get rid of the Component itself, you would just be changing the paricle emitter attached to the component
Your answer
Follow this Question
Related Questions
Laser Beam in Unity 0 Answers
Particle Rigid Body Dynamics without using rigid body dynamics 0 Answers
Use animated prefab model as a mesh in a particle system? 1 Answer
How to simply enable an objects particle system emission when that object collides with another? 1 Answer
How to change particles' view? 2 Answers