Class Destruction Cancels Another Class's Function

Hi Guys,

When a gameobject’s class calls a function from another class and is then destroyed the other class’s function stops. If I halt destruction of the object the other classes function works, otherwise it stops. Is this something in Unity I don’t know about?

Thanks in advance

GameObject’s Code

	//Checks to see if a player has been hit by the blast
	IEnumerator EndGame()
	{
		Collider[] hitColliders = Physics.OverlapSphere(transform.position, mnRadiusDet);
		
		int i = 0;
		bool hitPlayer = false;

		Instantiate(explosionPfx, transform.position, transform.rotation);
		StartCoroutine( camClass.Shake(100) ); // <<<<<This is where I call the function
		
		while (i < hitColliders.Length)
		{
			if(hitColliders[i].tag == "PlayerOne" || hitColliders[i].tag == "PlayerTwo" )
			{
				hitColliders[i].SendMessage("LoseHealth", 100);
				hitPlayer = true;
			}
			
			i++;
        }
		
		if(!hitPlayer)
		{
			Debug.Log ("Draw!");
		}

		camClass.LoadLevel(3.0f);
		Destroy(gameObject);

		yield return null;
	}

Other Object’s Code (Camera Class)

	void Update ()
	{
		if(moon  !bGameEnded)
		{
			// Handles the rotation of the camera to where the moon is...
			Vector3 camToMoonRot = (moon.transform.position - transform.position).normalized;
			Vector3 mixedRot = Vector3.Cross(transform.forward, (camToMoonRot*10));
			transform.rotation = Quaternion.Euler(mixedRot);
		}
		else
		{
			if(playerWon)
			{
				transform.rotation = Quaternion.LookRotation(playerWon.transform.position);
			}
		}
	}

	public IEnumerator Shake(float impactMagnitude) // <<<<<< This is the function from the other class I'm calling and stops.
	{
		
	    float elapsed = 0.0f;
		float duration = 1.0f;
	    int test =0;
	    Vector3 originalCamPos = Camera.main.transform.position;
	    
	    while (elapsed < duration)
		{
	        elapsed += Time.deltaTime;
			
			test++;
			float fadeFactor = 1 - (elapsed / duration);
			float offsetMultiplier = 10 * fadeFactor * impactMagnitude;
			float offsetx = offsetMultiplier * Mathf.Sin (test);
			float offsety = offsetMultiplier * Mathf.Cos (test);
			transform.position = new Vector3(
				transform.position.x + offsetx,
				transform.position.y + offsety,
				transform.position.z);
			
			yield return null;
	    }
		
	    Camera.main.transform.position = originalCamPos;
	}

You’re not calling the Shake function directly. Instead you create a coroutine. The coroutine is in fact a C# iterator (see here Iterators (C# and Visual Basic) | Microsoft Learn) and is a function of the first gameobject. So by destroying the gameobject you also destroy the coroutine.
The coroutine/iterator works by calling Shake until it reaches the yield. On yield Shake returns null to the calling coroutine and then continues running from that point, when the coroutine retrieves the next value in the iterator. When you destroy the calling gameobject, there’s no more coroutine to return to, so Shake never continues.

Awesome explanation thanks :). Know what I need now