How to call a function from another script - getting error "Object reference not set to an instance of an object"

Im having a problem calling a function from another script.

Ive looked around and came up with THIS and THIS, but I cant to get it to work.

I want to call the function AddScore() located in ScoreTextScript.js from BallHitTarget.js

BallHitTarget.js

var scriptName : ScoreTextScript;

function Update () {
    scriptName.AddScore();
}

ScoreTextScript.js

var score : int = 0;

function Update()

{

guiText.text = "Score: " + score;

}

function AddScore()

{
    score ++;

}

I keep getting the error "NullReferenceException: Object reference not set to an instance of an object BallHitTarget.Update () (at Assets\Standard Assets\Scripts\BallHitTarget.js:6)" with line 6 being scriptName.AddScore();

JS not being my strong point, so i'm sure someone else will chime in, but may i suggest that you're creating a new instance of ScoreTextScript not retrieving the one in use in the scene currently

Doc wise, this would be the one for direct calling: http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

psuedo code but, if you want to stay with the same type of system, you may just want to

//you would place the object that has the ScoreTextScript on it 
///in this var slot in the inspector

var scriptName : GameObject;

.....
//and access it via
scriptName.GetComponent(ScoreTextScript).AddScore();

if you're looking to build something a little more reusable and flexibile, i'd say look into one of the Messaging systems, http://technology.blurst.com/unityscript-messaging-system/ for javascript, or http://www.unifycommunity.com/wiki/index.php?title=CSharpMessenger_Extended for c# its a bit more of an initial learning curve, but a lot more flexible in the long run

This sounds like you've created the public reference variable, but you haven't actually assigned the reference. It's possible to do this with scripting, using GetComponent, however this is a task ideally suited to Unity's Dragged References.

First of all, are both scripts each placed on their respective GameObjects in the scene? I.E. presumably "BallHitTarget" should be placed on some sort of 'target' GameObject, and ScoreTextScript should be placed on a GameObject which has a GUIText component attached.

If so, it sounds like you may have just missed the last simple step which is to drag a reference from the Score Text object into the "ScoreTextScript" variable slot, on the target object.

To do this:

  • Select the target object
  • Now look at the BallHitTarget script in the inspector - you should see the variable 'scriptName' visible there*
  • Now drag the Score Text object from your hierarchy into that variable.

This creates a reference to the specific instance of "ScoreTextScript" which is attached to that gameobject.

By the way, "scriptName" isn't a very appropriate name for this var, and therein probably lies some of your confusion.

You might want to consider renaming the "scriptName" var to something which better suits its contents, such as "scoreManager", and perhaps you should even rename the ScoreTextScript itself to "ScoreManager" (with a captial S, to fit Unity's scripting conventions) - since it also keeps track of the score as well as just displaying it.

Your final scripts would then look like this:

BallHitTarget.js

var scoreManager : ScoreManager;

function Update () {
    scoreManager.AddScore();
}

ScoreManager.js

var score : int = 0;

function Update() {
    guiText.text = "Score: " + score;
}

function AddScore() {
    score ++;
}

hi , i problem too

i have script : MoneySystem.js

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

function Update() { Debug.Log(money);

}

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

}

and other script have : CharacterDamej.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.AddMoney();

	}
}

but same error : NullReferenceException: Object reference not set to an instance of an object

:frowning:

While you have cast the scriptname, the problem seems to be that you haven’t actually told Unity exactly what it is.

Try adding this to your code.

function Awake () {

scriptName = GameObject.Find("Name_Of_Game_Obj_Where_script_Is_Atached").GetComponent( ScoreTextScript);

}

Then you can call whatever you want to from your other scripts.

Also, if BOTH scripts are attached to the same GameObject, then you can use the SendMessage function.

SendMessage("AddScore");

More information on SendMessage here Unity - Scripting API: GameObject.SendMessage

Hey! I know its kinda late, but I was searching for the same problem and I found a diferent solution, what you really want to do it’s this:

yourGameObject.GetComponent<TheNameOfTheScript>().yourMethodHere();