How to call a function from another script - getting error Object reference not

hi all
i have 2 script

1.MoneySystem.js

#pragma strict
static var money : int = 0 ; // for money systemfunction


function Update() {

}

function AddMoney()
{
	money = money + 20 ;
	Debug.Log(money);

}

and

2.CharacterDamage.js

var hitPoints = 100.0;
var deadReplacement : Transform;
var dieSound : AudioClip;
var moneySystem : MoneySystem ; // for money system
function ApplyDamage (damage : float) {
	// We already have less than 0 hitpoints, maybe we got killed already?
	if (hitPoints <= 0.0)
	{
		return;
	}

	hitPoints -= damage;
	//expand enemy search radius if attacked outside default search radius to defend against sniping
	transform.GetComponent(AI).attackRangeAmt = transform.GetComponent(AI).attackRange * 3;
	
	if (hitPoints <= 0.0)
	{
		Detonate();
		//money system
		moneySystem = this.gameObject.GetComponent(MoneySystem);
		moneySystem.AddMoney();
	}
}

function Detonate () {
	// Destroy ourselves
	Destroy(gameObject);

	// Play a dying audio clip
	if (dieSound)
		AudioSource.PlayClipAtPoint(dieSound, transform.position);

	// Replace ourselves with the dead body
	if (deadReplacement) {
		var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
		
		// Copy position  rotation from the old hierarchy into the dead replacement
		CopyTransformsRecurse(transform, dead);
	}
}

static function CopyTransformsRecurse (src : Transform,  dst : Transform) {
	dst.position = src.position;
	dst.rotation = src.rotation;
	
	for (var child : Transform in dst) {
		// Match the transform with the same name
		var curSrc = src.Find(child.name);
		if (curSrc)
			CopyTransformsRecurse(curSrc, child);
	}
}

but dont work and give a error :
NullReferenceException: Object reference not set to an instance of an object

how can fix it ?!

Have you added the MoneySystem script to the gameObject?

Select the gameobject that holds the CharacterDamage, and drag the gameobject that holds the moneysystem in the “MoneySystem” field

yes , "MoneySystem"attached to player

I do not understand what you mean

Who can help me?

http://unity3d.com/learn/tutorials/modules/beginner/editor/the-inspector

i want do it with code

MoneySystem.js attached to player
and
CharacterDamage.js attached to enemy

in CharacterDamage.js
i create a varibale
var moneySystem : MoneySystem ; // for money system

and with code i try to access to AddMoney Function

//money system

moneySystem = this.gameObject.GetComponent(MoneySystem);

moneySystem.AddMoney();

but dont work :frowning:

i change code to this :

var hitPoints = 100.0;
var deadReplacement : Transform;
var dieSound : AudioClip;
//var moneySystem : GameObject ; // for money system
function ApplyDamage (damage : float) {
	// We already have less than 0 hitpoints, maybe we got killed already?
	if (hitPoints <= 0.0)
	{
		return;
	}

	hitPoints -= damage;
	//expand enemy search radius if attacked outside default search radius to defend against sniping
	transform.GetComponent(AI).attackRangeAmt = transform.GetComponent(AI).attackRange * 3;
	
	if (hitPoints <= 0.0)
	{
		Detonate();
		//money system
		var ms : MoneySystem;
		ms = "Player".GetComponent("MoneySystem");
		ms.AddMoney();
	}
}

but …
new error has :
MissingMethodException: Method not found: ‘System.String.GetComponent’.

i can fix it by this code

		ms = GameObject.Find("Player").GetComponent(MoneySystem);

and also u can 1 line code replaced for all of code in CharacterDamage

 GameObject.Find("Player").GetComponent(MoneySystem).AddMoney();

:smile:
tan Q all :x:grin: