- Home /
coroutine(Firelaser)couldn't be started
please help me to clear this error(coroutine(Firelaser)couldn't be started).i get it when i play game.here is the script.laser is not working .help plz its my final year project
LASER SCRIPT
using UnityEngine; using System.Collections;
public class laserscript : MonoBehaviour {
LineRenderer line;
void Start ()
{
line=gameObject.GetComponent<LineRenderer>();
line.enabled=false;
Screen.lockCursor=true;
}
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
StopCoroutine("FireLaser");
StartCoroutine("FireLaser");
}
}
IEnumerator firelaser()
{
line.enabled=true;
while(Input.GetButton("Fire1"))
{
Ray ray=new Ray(transform.position,transform.forward);
line.SetPosition(0,ray.origin);
line.SetPosition(1,ray.GetPoint(100));
yield return null;
}
line.enabled=false;
} }
Answer by CHPedersen · Sep 18, 2014 at 06:58 AM
Your naming is off. The method you wish to start is called "firelaser", but you're starting it as "StartCoroutine("FireLaser")".
StartCoroutine is case sensitive. You have to make sure the method name matches exactly.
Answer by zharik86 · Sep 18, 2014 at 06:59 AM
Change name function Coroutine. In Update you stop and start function by name FireLaser, but coroutine is firelaser. Change code:
IEnumerator FireLaser() { //change name
line.enabled=true;
while(Input.GetButton("Fire1")) {
Ray ray=new Ray(transform.position,transform.forward);
line.SetPosition(0,ray.origin);
line.SetPosition(1,ray.GetPoint(100));
yield return null;
}
line.enabled=false;
}
I hope that it will help you.