- Home /
How to Instatiate a particle system on a moving object
I'm stuck. The situation is that game objects; which are challenges; randomly generate off-screen and come towards the player. On those game objects are coins. When the player runs into the coin a particle system is supposed to play.
I've created a prefab that contains the particle system and I'm trying to instantiate the particle system on the position of the coin when it enters the scene and runs into the player. The method I was trying in the code below is to get the Coins position in the script via vector 3. Store it, then call that position in the particle system instantiation. I don't know how to do this here is what I have.
In the inspector
In the Code
Just one thing. Instantiate dont requeire the "as GameObject". It will instantiate the same type as the `prefab used.
Answer by cesarcaos · Jan 16, 2019 at 04:01 PM
Hi!, you can call other.transform.position for the position and other.transform.rotation for the rotation to instantiate the CoinParticleSystem. that should work
Answer by badadam · Jan 16, 2019 at 05:23 PM
If you don't want to assign the instantiated gameobjects to a variable, use the script below
Instantiate(CoinParticleSystem,CollectableCoinPosition.position,Quaternion.identity);
If you use "as GameObject" ,you should assign the instantiated gameobject to a variable.
GameObject ob=Instantiate(CoinParticleSystem,CollectableCoinPosition.position,Quaternion.identity ) as GameObject;
Thanks man the code above works! The particle system is instantiated however the real problem is getting it to instantiate on the coin. I can't put the particle system on the coin because if the game starts the coin hasn't generated therefore I get a null reference exception in the inspector.
GameObject ob=Instantiate(CoinParticleSystem**,CollectableCoinPosition.position**,Quaternion.identity ) as GameObject;
So where the argument where it says CollectableCoinPosition, how would I make the variable to where it references the coin game objects position in real time? Is that even possible?
Your answer
Follow this Question
Related Questions
How do i move an object relative to another objects axis. 2 Answers
Confuse about transform.InverseTransformPoint 1 Answer
Name of the prefab instantiated has clone suffixed 1 Answer
Is it possible to set one particle system equal to another through script? 0 Answers
Particle System fails to emit() after last particle dies 3 Answers