Particle system instantiation problem

I’m currently following the lecture “Hit VFX” (#158) of the “Complete C# Unity Game Developer 3D (Updated To Unity 6)” course and ran into some issues in the last 20 seconds of the video.

Here is the issue:

The instructor sets the Stop Action of the Hit VFX Game Object to “Destroy”. This makes sense as it is supposed to remove all the instantiations of the VFX game object copies when the particle systems have finished playing. However, by doing so the original VFX game object also plays on awake and destroys itself in a matter of seconds (as it is instructed to). This leads me to an error, since now the script is trying to instantiate a game object which does no longer exist.

The error is the following:
“MissingReferenceException: The object of type ‘UnityEngine.GameObject’ has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. Parameter name: data”

I have tried disabling the original VFX game object, but that will lead to copies being instantiated as disabled as well. I tried unchecking the “Play On Awake” checkbox, but that led to all instantiations to not play on awake. Therefore, in both these attempted fixes the copies would not play when instantiated.

How can I instantiate and destroy the copies after the original has already been played and destroyed?

Hi MellowToad,

Good job on analysing the problem. :slight_smile:

Could you please share screenshots of what you did in Unity? At the moment, I do not know what you mean by “original VFX game object”.

Correct me if I’m wrong but I understood that more VFX game objects get instantiated, and the original VFX game object seems to be the blueprint. Since prefabs in the Assets folder cannot be destroyed, I assume that you instantiate a copy of a game object in the Hierarchy instead of a copy of a prefab. Given I’m right, this could explain the bug you described here.

Did you do what Steven does as of the 4:58 timestamp? Or did you drag and drop the object from the Hierarchy?

Hi Nina, thank you for the help. You are right, it worked.

I deleted the “original” VFX game object from the hierarchy and dragged the prefab from the asset folder into the serialised field instead. Now the VFXs get instantiated and destroyed as they should.

Many thanks again!

You’re welcome. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms