how do i stop a function?

hey everyone,

I have a situation where an enemy automatically runs to a target when it is within distance.

When it is within distance, i call a function named RunToTelePole. The player then has to shoot the enemy and i need him to stop wherever his is and then run away.

For the “most part” it works, but he still sticks around the switch until everything in RunToTelePole is finished running.

any input on how to get this to completely ignore the function when the enemy is hit?

It would be helpful if you shared your script with us.

put a boolean variable enableShooting to mark true = shooting ; false = run away

inside RunToTelePole , add

if ( enableShooting ) {}
else {}

here is my script, im not exactly sure how to go about the boolean.

var ScoreCard : Transform;
var poop : Transform;
var anim = 0;
var script : person_Move;
var score = 10;

var ShockPoints = 0.0;

var PoopedTexture : Texture;
var BodyChild : Transform;

/// Telephone Poles
var TelePole1 : GameObject;
var TelePole2 : GameObject;

var RunSpeed = 1.0;
///// Run Range
var RunRangeMax = 0.0;
var RunRangeLow = 0.0;

/// Sidewalks
var StreetDiv : GameObject;

var PoopedOn = 0; //// 0 no 1 yes

/// shock
var ShockRight : GameObject;
var ShockLeft : GameObject;

var WireHeight = 0.0;

/// Avoid Characters 
var AvoidScript : Person_Avoid;


///rotation
var StartRot_x = 0.0;
var StartRot_y = 0.0;
var StartRot_z = 0.0;


var TelDist = 0.0;

function Start()
{
StartRot_x = transform.localEulerAngles.x;
StartRot_y = transform.localEulerAngles.y;
StartRot_z = transform.localEulerAngles.z;

///find objects
TelePole1 = GameObject.Find("TelePole1");
TelePole2 = GameObject.Find("TelePole2");
StreetDiv = GameObject.Find("StreetDiv");
ShockRight = GameObject.Find("ShockGuide");

///person avoid
AvoidScript = GetComponent("Person_Avoid");


script = GetComponent("person_Move");
}


function Update(){

if(PoopedOn == 1)
{
StopCoroutine("RunToTelePole");
Pooped_On();
}



if(transform.position.z<RunRangeMax  transform.position.z >RunRangeLow  PoopedOn == 0)
{
RunToTelePole();
}
}



function OnCollisionEnter(collision : Collision){



if(collision.gameObject.tag =="poop")
{
PoopedOn = 1;
}



}


function Pooped_On ()
{
transform.rotation.x =StartRot_x;
	transform.rotation.y = StartRot_y;
	transform.rotation.z = StartRot_z;
animation.CrossFade("Run", 0.1);
RunSpeed = 1.0;
}


function RunToTelePole(){

script.speed = RunSpeed; 
if(transform.position.x>StreetDiv.transform.position.x)
{
AvoidScript.Avoid_On = 0;
animation.CrossFade("Run", 0.1);
transform.LookAt(TelePole1.transform);
//transform.position = Vector3.Lerp(transform.position, TelePole1.position,Time.deltaTime*RunSpeed);
	if(TelePole1)
	{
	var Tele1Dist = Vector3.Distance(TelePole1.transform.position,transform.position);
		if(Tele1Dist <TelDist)
		{
		
		animation.CrossFade("Fixxing",0.1);
		RunSpeed=0;
		yield WaitForSeconds(1);
	//	POM_script.POWER =POM_script.POWER-ShockPoints/2;
		ShockRight.transform.position.y = WireHeight;
		yield WaitForSeconds(2);
		ShockRight.transform.position.y = -1;
		
	//	animation.CrossFade("Walk",0.1);
				transform.rotation.x =StartRot_x;
	transform.rotation.y = StartRot_y;
	transform.rotation.z = StartRot_z;
	animation.CrossFade("Walk",.01);
script.speed = .4; 
AvoidScript.Avoid_On = 1;
		//	PoopedOn = 0;
		}
	}
}

if(transform.position.x<StreetDiv.transform.position.x)
{
AvoidScript.Avoid_On = 0;
animation.CrossFade("Run", 0.1);
transform.LookAt(TelePole2.transform);
//transform.position = Vector3.Lerp(transform.position, TelePole1.position,Time.deltaTime*RunSpeed);
	if(TelePole2)
	{
	var Tele2Dist = Vector3.Distance(TelePole2.transform.position,transform.position);
		if(Tele2Dist <TelDist)
		{
		animation.CrossFade("Fixxing",0.1);
		RunSpeed=0;
		yield WaitForSeconds(1);
		//POM_script.POWER =POM_script.POWER-ShockPoints/2;
		ShockRight.transform.position.y = WireHeight;
		yield WaitForSeconds(2);
		ShockRight.transform.position.y = -1;
		
	//	animation.CrossFade("Walk",0.1);
				transform.rotation.x =StartRot_x;
	transform.rotation.y = StartRot_y;
	transform.rotation.z = StartRot_z;
	animation.CrossFade("Walk",.01);
script.speed = .4; 
AvoidScript.Avoid_On = 1;
			//PoopedOn = 0;

		}
	}
}

}

not sure for js, but i would use an event for this eventually, send by your enemy when it get hit to call somthing to stop your function.

yea i tried this StopCoroutine(“RunToTelePole”); command but it doesnt seem to do anything

when he is hit, the animation changes but everything else stays the same, so he doesnt run away he runs in place until the whole funcitno is finished

StopCoroutine(“RunToTelePole”); will only work if the method was called using StartCoroutine(“RunToTelePole”);

Keep in mind that StopCoroutine will also only stop the method when that method reaches a yield statement.

well keeping on the event idea you could have a flag variable in your coroutine and loop though like

while(flag)
{
//do stuff
yield return null;
}

and at any time if from an event or whatever you want set your flag false in that case it will get our of the coroutine without need to call StopCoroutine

something in this fashion I mean

using UnityEngine;
using System.Collections;

public class CoroutineTest : MonoBehaviour
{
        public bool flag = true;
	public int i = 0;
	
	void Start()
	{
		StartCoroutine(Test());
	}
	
	IEnumerator Test()
	{
		Debug.Log("Enter Coroutine !");
		
		while(flag)
		{
			i++;
			yield return null;	
		}
		
		Debug.Log("out of coroutine");
	}
	
	void LateUpdate()
	{
		if(Input.GetMouseButtonDown(1))
			flag = false;
	}
}

o man, by bad, stupid mistake lol fixxed the problem, i was just not adjusting the speed properly. works like a charm now thanks eveyrone

hehe yes i do not count myself stupid mistake i do in a day :smile:…glad you figure out :slight_smile: