Play particlefx when objects collide

I am following the Runner tutorial. Now, I reached the end of it and trying to add the an effect when the frog collects the coins. I created a particle called coin.particlefx and put it inside the folder particles already but I am sure how to play it from the script file. This is my coin.script:

-- coin.script
function init(self)
	self.collected = false
end

function on_message(self, message_id, message, sender)
	if self.collected == false and message_id == hash("collision_response") then
		self.collected = true
		particlefx.play("/particles/coin#emitter") 
		msg.post("#sprite", "disable")
	elseif message_id == hash("start_animation") then
		pos = go.get_position()
		go.animate(go.get_id(), "position.y", go.PLAYBACK_LOOP_PINGPONG, pos.y + 24, go.EASING_INOUTSINE, 0.75, message.delay)
	end
end

Here’s the screenshot of the particle and the error message

I have checked the Defold manual and video tutorial, but there is nothing mentioning on how to play particlefx from the script.

Anyway, can you point out why I could not play the particle effect in this particular case?

Thank you.

1 Like

Good, this is step 1

This is irrelevant. Where you put the file among your assets has nothing to do with how you play it. Have you attached the coin.particlefx component to a game object? Once you have it attached to a game object you play it like you did above:

particlefx.play("/id_of_your_game_object#id_of_your_particlefx_component")
2 Likes

Finally, I have managed to make it work. I added the particlefx to the coin game object coin.go and gave it ID of coin_fx and from the coin.script I called like this:

particlefx.play("#coin_fx")

and it works. However, when I used the following syntax, it did not work:

particlefx.play("main:/level/objects/coin#coin_fx") 
particlefx.play("/level/objects/coin#coin_fx") 

Here’s screenshot of the coin game object after adding particlefx.

The particlefx in action:

1 Like

Well, you spawn your coins using a factory right? That means that the generated coins will have varying ids along the line of “instance0”, “instance1” etc. You can use the other_id from the collision message to create a valid URL. (But since you mentioned that you solved it I assume you know this already) :slight_smile: