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
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();
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 ++;
}
#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