- Home /
Reference Function Trouble
//first script function TakeDamage(amount : int) { var difference=MaxHp/amount; if(difference<=10) animation.CrossFadeQueued("Heavy Damage"); else animation.CrossFadeQueued("Light Damage"); Hp-=amount; }
//this is in a different script
function Awake()
{
var charac=GameObject.Find("Player");//get object.
var playerinfo=charac.GetComponent("PlayerObj");//get script
}
if(Input.GetButton("Fire1"))
{
playerinfo.TakeDamage(25);
}
I keep getting a code that says : Unknown identifier: 'playerinfo'. Could someone tell me what I'm doing wrong?
Answer by DaveA · Aug 14, 2011 at 06:43 PM
var playerinfo must be outside the functions, globally available to all the functions. Put it above all else
Answer by darkxcalibur · Aug 14, 2011 at 06:52 PM
why not just use send message?
and also try this :
var charac : GameObject; var playerinfo : PlayerObj; //your script name i think
function Awake() { charac=GameObject.Find("Player");//get object. playerinfo=charac.GetComponent("PlayerObj");//get script }
//this goes in he update if(Input.GetButton("Fire1")) { playerinfo.TakeDamage(25); }
I'm now getting an error that says :
Object reference not set to an instance of an object
I know what it means, but I'm still at a loss as to how to fix it.. since I thought it was referenced o_O
Your answer
Follow this Question
Related Questions
Can References to Functions Be Stored in an Array? 2 Answers
Referncing variables such as OnTriggerEnter(Collider other) thumb rule? 1 Answer
General function knowledge 1 Answer
Adding a reference to a project not working 1 Answer
Getting a reference to a group of ScriptableObjects implemented at runtime? 0 Answers