Fail to start a coroutine in another C# file

I am reading the DeepPixel book, the examples were written in JavaScript, I am converting them to C# but encounter too much roadblocks.

I have 2 files, one is a GameObject call “gameManager”, the GameObject has a C# script call “gameMamager.cs”

Inside “gameManager.cs”, there is a method call “destroyPlayer()”

	IEnumerator destroyPlayer (Vector3 dpos) {
	GameObject Player1 = Instantiate (playerDeathObj, dpos, playerDeathObj.transform.rotation) as GameObject;
	player1.animation["death"].speed = 3.5f;
	yield return new WaitForSeconds (0.5f);
	Destroy (player1);
}

Another file call “shipController.cs”, the

	bool playerInvincible = false;
GameObject gameMgObj;

// Use this for initialization
void Start () {
	gameMgObj = GameObject.Find ("gameManager");
}

void OnTriggerEnter (Collider enemy) {
	if (enemy.tag == "enemyLaser" || enemy.tag == "enemy") {
		Destroy (this.gameObject);
	}
	
	Component script1 = gameMgObj.GetComponent ("gameManager") as Component;
	Vector3 pPosition = transform.position;
	StartCoroutine (script1.destroyPlayer (pPosition));
	script1.respawn = true;
	script1.playerLives -= 1;
}

Unity throw an error:

error CS1061: Type UnityEngine.Component' does not contain a definition for destroyPlayer’ and no extension method destroyPlayer' of type UnityEngine.Component’ could be found (are you missing a using directive or an assembly reference?)

gameMgObj is the “gameManager” object, and script1 is a “gameManager.cs” component in gameMgObj, I want to invoke the IEnumerator destroyPlayer in gameMamager.cs from shipController.cs, but I can’t figure out how to call it.

Appreciated if someone can help or advise.

The compiler is right: Component is a standard Unity object, and doesn’t have a destroyPlayer function. Who has this function is a Component descendant, the script gameManager. You must declare the variable script1 as gameManager, so Unity will know that the script referenced by script1 has such function. You could change the script1 assignment line like this:

gameManager script1 = gameMgObj.GetComponent<gameManager>();

This version of GetComponent looks for a component of type gameManager (the script, not the object).