Two scripts (Enemy health and Experience)

Hello, I need to do when the enemy has 0 health so I add 10 Experience.
Experience.js

var curExp : float;
var maxExp : float;
var level : float = 1;
var expTexture : Texture;

function Update () 
{
	if (curExp >= maxExp)
	{
		// It means we have leveled up
		print ("LevelUp" + level);
		curExp = 0;
		LevelUp();
	}
	[COLOR="red"]curExp ++;[/COLOR]
}

function OnGUI()
{
	GUI.DrawTexture(Rect(300, 500, curExp, 30), expTexture);
}

function LevelUp()
{
	// Increase our max exp
	maxExp += 20;
	level ++;
}

EnemyHealth.cs

using UnityEngine;

using System.Collections;



public class EnemyHealth : MonoBehaviour {

	public int maxHealth = 100;

	public int curHealth = 100;

	

	public float healthBarLength;

	

	void Start() {

		healthBarLength = Screen.width / 2;

	}

	

	void Update() {

		AddjustCurrentHealth(0);

	}

	

	void OnGUI() {

		GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth + "/" + maxHealth);

	}

	

	public void AddjustCurrentHealth(int adj) {

		curHealth += adj;

		

		if(curHealth < 0)

			curHealth = 0;

		

		if(curHealth > maxHealth)

			curHealth = maxHealth;

		

		if(maxHealth < 1)

			maxHealth = 1;

		

		[COLOR="red"]if(curHealth <= 0) [/COLOR]

		{ 

   			Destroy(gameObject); 

		}

		

		healthBarLength = (Screen.width / 2 ) * (curHealth / (float)maxHealth);

	}

	

}

The best example I can give you I wrote out for another person on the site. It is not exactly what you are looking for but the concept of how object talk to each other is really what you are after.

http://forum.unity3d.com/threads/98050-Scripting-GURU-Bonus-Crates-Scoring-System?p=663166&viewfull=1#post663166

At some point, your player will need to talk to the target. Or worse your player’s bullet will have to talk to the target, and then talk to the player. It gets a little hairy after that, then Kenny had to go live in a foster home for a while.

The big point is that whatever you do. An object will need to be tagged so that at some point the person who did the killing would know that he got some points out of it.

Perhaps even going to think I’m stupid, but I understand it.

I don’t really understand your question (well actually you didn’t ask any questions). Can you please strip down your code and ask something specific? I’d be glad to help if I can.

I want to give curExp + +; the script with EnemyHealth more here

 	if(curHealth <= 0) 

		{ 
                        [COLOR="red"]Here[/COLOR]
   			Destroy(gameObject); 

		}

And to make it work

On some script attached to the player, have a listening function, so that in that code there, you can just use a SendMessage()

Please edit my script. I do not know much in this.

On some script attached to the player, have a listening function, so that in that code there, you can just use a SendMessage()

so for eg: Your player object has a script attached to it somewhere that has this code …

function AddExperience(experienceValue : int){
    exp+= experienceValue;
}

And than in your code for the enemies health

if(curHealth <= 0) 

{ 
playerObject.SendMessage("AddExperience", monsterExperiencePoints);
Destroy(gameObject); 

}

The only thing you should be aware of , is that you would need to have a variable created in the monsters script that holds a reference to the player. playerObject in this case , is the variable i would have created. Hope that makes sense , also , im sure theres a better way to do this , but this works aat least.

SendMessage shouldn’t be necessary here since the target for the functionality shoudl be known via a reference so a direct call should be possible. I still need a lot more information though, sorry. I will try to explain a possible “pattern” for coding this.

  1. Create a script named “Player” with all the attributes you want, health, exp, for this example, lets say you carry gold, so make a gold variable as well … Add this to either your FPS camera or whatever makes sense for your game (even an empty gameobject works - just consider you might want to add more scripts later on the same GameObject)

  2. Create an “Enemy” script with all the attributes you want, health, exp, for this example, let’s say you get “gold” as a reward, so add “gold” as well. Also add a function “OnDeath”. OnDeath() should destroy the enemy (see our product PoolManager if you want to despawn an instance instead of destroying it, but “pooling” is another discussion)

  3. On the PLAYER script, make some function to handle getting a target and attacking. When you get the target, cache the Enemy component. This function, I’d call it “OnAttack()”, should perform an attack then check the Enemy.health, and if it is time to die, add Enemy.gold to Player.gold, then call Enemy.OnDeath()

You should now have a system to attack an enemy, set its damage, kill it and get a reward.

This is a simple pattern and there are many variations. For example, it is more advanced but probably better to have the player call a function Enemy.OnHit() and pass a custom struct with all the various effects an attack can have (direct damage, damage-over-time, slow, disease, whatever, all in one struct), then when it dies, it could access the Player via a static method to grant a reward before dieing. If this is a multi-player game then static access wouldn’t work because you would have more than one player, in which case you would probably want to pass a reference back to the player to OnHit().

Again, there are many ways to approach this.