Can't make coroutine work

so my script works fine without the coroutine:

using UnityEngine;
using System.Collections;

public class jointbreak : MonoBehaviour 
{

	private ParticleRenderer myParticleRender;
	private ParticleEmitter myParticleEmitter;

	void Start ()
	{
		myParticleRender = GetComponent<ParticleRenderer>();
		myParticleEmitter = GetComponent<ParticleEmitter>();
	}

	Void OnJointBreak(float breakForce)
	{
		myParticleRender.enabled = true;
		myParticleEmitter.enabled = true;
	}
}

But unfortunately I need a delay after enabling to disable the particles again, and this is my script now:

using UnityEngine;
using System.Collections;

public class jointbreak : MonoBehaviour 
{

	private ParticleRenderer myParticleRender;
	private ParticleEmitter myParticleEmitter;

	void Start ()
	{
		StartCoroutine(OnJointBreak(float breakForce));
		myParticleRender = GetComponent<ParticleRenderer>();
		myParticleEmitter = GetComponent<ParticleEmitter>();
	}

	IEnumerator OnJointBreak(float breakForce)
	{
		myParticleRender.enabled = true;
		myParticleEmitter.enabled = true;
		yield return new WaitForSeconds(10.0f);
		myParticleRender.enabled = false;
		myParticleEmitter.enabled = false;
	}
}

It looks like there is some issue in …(OnJointBreak(float breakForce));… right after float XD any idea?

2 Answers

2

You are starting the coroutine before you are initializing ‘myParticleRenderer’ and ‘myParticleEmitter’. Take line 12, and move it down below line 14 and your code should work.

Still not working it says the same " Assets/scripts/jointbreak.cs(14,60): error CS1525: Unexpected symbol breakForce', expecting .' "

–

You need to remove the "float" from the call. StartCoroutine(OnJointBreak(breakForce)); The question then becomes, where are you getting breakForce from? Is it supposed to be a member of jointbreak? It doesn't look like it's being used in the function you're passing it to.

–

Instead of:

 StartCoroutine(OnJointBreak(float breakForce));

Use:

 float breakForce = 5.0f;
 StartCoroutine(OnJointBreak(breakForce));

With whatever value of breakForce is appropriate.

The issue is that instead of passing a value to the OnJointBreak function you are declaring a new variable.

it work thanks

–