#There is no Component attachted to X

1 messages · Page 1 of 1 (latest)

fallen urchin
#

In the Awake function I use

particleSystem = GetComponent<ParticleSystem>();

To get the particle system which is a child of my current component.
But I'm getting an exception saying

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

The script is sitting on the object which has the arrow pointint on it

vivid axle
#

where do you run that script on? if it is on Gun, you see the Gun Particles are in a child object, not the current one. then you could use GetComponentInChildren

lucid prism
#
  1. Components cannot have children. Your terminology might be off here; you probably meant "a child of my current transform"
  2. GetComponent only searches for a component on the same object that it is called on. If your ParticleSystem is on Gun Particles, but your script sits on Gun, GetComponent will not find it like this.
  3. You should reference components directly instead of relying on GetComponent whenever possible, so I suggest you make a [SerializeField] private ParticleSystem particleSystem; in your gun script, and manually drag your Gun Particles into it.
fallen urchin
#

Oh yeah I understood the function wrong.

#

I'll use a direct reference then. Thank you