Stoping a function in mid-script.

Hello there everyone.

I have a problem, need some pointer and opinion. I’m currenctly doing a “skip function” for animation in my app. What it does is, it skip the animation and play the dailog rather than you have to wait for the animation to finished and then the dailog play.So I made this script. There’s two part for the script. One is for skipping the animation, That one work well. Second one is the audio script, having trouble with this one :

#pragma implicit
#pragma downcast
#pragma strict

var voice01 : AudioClip;
var voice02	: AudioClip;
var voice03	: AudioClip;

//Contents
var Content01 : GameObject;

var text : GameObject;
var playAgain : GameObject;

var speek : boolean = false;
static var playOnce : boolean = false;
static var playOnce2 : boolean = false;

var hit : RaycastHit;

function Update()
{
	contentOff();
	
	if(playOnce == false)
	{		
		if(GetComponent.<SkipAnimation>().skip == true)
		{
			skiped();
		}
	}
	
	if(playOnce2 == false)
	{		
		narate();
	}
	
	playMore();
}

function skiped()
{
	//Debug.Log("sini");
	playOnce = true;
	audio.PlayOneShot(voice03);
	text.active = true;
	yield WaitForSeconds(20);
	speek = true;
}

function narate() //The narator tell the story
{
	//Debug.Log("sana");
	playOnce2 = true;
	//Voice Narator and text
	yield WaitForSeconds(9);
	audio.PlayOneShot(voice03);
	text.active = true;
	yield WaitForSeconds(20);
	speek = true;
}

function contentOff()
{
	yield WaitForSeconds(9);	
	Content01.SetActiveRecursively(false);	
}

function playMore() //Repeat the special dailog
{
	if(speek == true)
	{
		playAgain.active = true;
		speek = false;
	}
	else
	{
		speek = false;
	}
	
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);		
	
	if (Input.GetMouseButtonDown (0))  //Returns true during the frame the user touches the object
	{
		if (Physics.Raycast (ray, hit, 100)) 
		{	
			if(hit.collider.tag == "0") 
			{
				//Voice Upin  Ipin
				playAgain.active = false;
				audio.PlayOneShot(voice01);
				yield WaitForSeconds (voice01.length);
				audio.PlayOneShot(voice02);
				yield WaitForSeconds (voice02.length);
				playAgain.active = true;
			}
		}
	}
}

How do I stop the “narate” function if the “skipped” function is running? Because now if the “skipped” function is running the “narate” function still run.

Bump

Uhh, stop calling it to stop it? And if it’s playing a sound just do sound.Stop();

OK, so I am reading this, trying to understand what you are doing…

All of the functions you are calling have a yield command embedded into them. the yield command is omitted from the script, so it never happens. Think of it this way. I am in an update… I want to wait 5 seconds… So I put a yield statement there. Well, if you really want Unity to stop working for 5 seconds while it waits for the yield command to finish… You get the point.

Yields work in coroutines. A coroutine is a process that runs beside the updates and such and work a little like this:

do something
yield for a frame
do something else
yield for 5 seconds
do one last thing
quit or rinse and repeat

If you are a bit confused or it just doesn’t fit into your scheme, you will need to do some advanced time editing in the Update:

if step1 then we can set step2 or step3. When we set step 2, we set a time start variable for step 2

If we are in step 2 and the time start variable + wait duration > time.time then we can do step 2. We can set step 4 or 5.

rinse and repeat

By far, the coroutine would be a simpler solution to keep track as you could write it logically and in order yielding as you need.