Accessing data members from different script


What I am trying to achieve is give points to the player when an enemy is destroyed.

In my enemy script I have a function:

public int enemyfallen = 0;
	public int EnemyDestroyed()

		if (health <= 0) 
			_idx = (int)(Random.value * blowup.Length);
			Transform destroyparticle = Instantiate(blowup[_idx], enemylocation, Quaternion.identity) as Transform;


			Destroy(gameObject, 5.0f);

		return enemyfallen;

In my player script I would like to give scores whenever an enemy is destroyed.
How can I setup this relationship between the enemy and the player?
Thank you.

Assuming that in order for an enemy to be destroyed, a player have to inflict damage. Here’s an example based on what I think you desire.


	playerScript player;//Reference of playerScript
	int health = 10;//Enemy's health

    // To be called by the player.
	public void inflictDamage(playerScript ps){// get reference of playerScript
		player = ps;//set the local playerScript variable to the received playeScript
		health -= player.damage;//reduce enemy health base on player's damage

	void checkEnemyHealth()
		if (health <= 0) 
			Destroy (gameObject);//Give player the data before destroying the enemy gameObject


public int damage =5;
public int score=0;
public int enemySlain=0;

public void attack(enemyScript enemy){//Get reference of enemyScript
	enemy.inflictDamage(this);//calls enemy public function with parameters as the playerScript(this)

I have no idea how you want the player to attack but an example would be:

void OnTriggerEnter(Collider e){
		attack(e.transform.GetComponent<enemyScript>());//Call playerScript's attack with the parameter as the enemyScript of which ever enemy it is attacking.

This would be placed within the playerScript. Assuming that it has a Rigidbody and a collider(for OnTriggerEnter() to work). If you walk to an enemy(with a collider and has a tag==“Enemy”) the player will get reference to the enemyScript and call its public function, inflicting damage on itself(the enemy)