- Home /
How to instanciate a particle system prefab to a certain moving game objects position?
The obstacles which have coins on them are moving toward the players. When the player collects the coin the coinparticlesystem prefab plays on the coins position before the coin game object is destroyed. The only problem is is that I don't know how to instatiate something on an object that is moving here is my code so far.
Since 2018.1 the Particle System (PS) offers the sub emitter spawning condition of “Trigger” so you can attach an empty local PS (use ring buffer mode if you’re using 2018.3), enable Trigger module to Callback on Enter and parent your coin PS to this spawning PS as a Trigger Sub Emitter.
Answer by ifurkend · Jan 07, 2019 at 01:36 AM
The setup I mentioned in my comment doesn't require any scripting. You may reference my video. That "SubEmitter0" is supposed to be your coin particle effect. https://www.youtube.com/watch?v=ijWWQoYdc8c
Thanks man I apreciate it. I'll test it out next time I get on unity and let you know how it goes. Again thanks
EDIT: I did exactly what you did in your video, and it didn't work for me I have know idea what I'm doing wrong. :(
Answer by Magso · Jan 07, 2019 at 12:27 AM
if this code is on the coin then it's
Instantiate(CoinParticleSystem, transform.position, Quaternion.identity);
The objects are instatiated shortly after the game plays, therefore wouldn't I get a null refrence exception if I have the script on a coin that isn't generated yet?
"When the player collects the coin the coinparticlesystem prefab plays on the coins position before the coin game object is destroyed." Led me to believe that the coins we're already in the scene and each of them had a script with the variable CoinParticleSystem assigned to a prefab. If OnTriggerEnter2D is called on a different object before the coins are instantiated and it attempts to find them, that would cause a null reference exception.
Sorry about that, I didn't clarify that very well. I initially did exactly what you said prior thats why I just made a particle system prefab that remains a constant
Your answer
![](https://koobas.hobune.stream/wayback/20220612185237im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is their any way to save a particle system when it's not instantiated yet? 2 Answers
How to instantiate an object with scripts? 1 Answer
How to Play ParticleSystem from AnimationEvent? 0 Answers
How to edit particle system size over lifetime curve with script? 0 Answers
Change particle size based on how near it is to a given point or height 2 Answers