- Home /
Question by
Vexintgames · Aug 23, 2014 at 06:23 PM ·
gameobjectgetcomponentvariables
Getting errors when referencing a variable.
I have a script attached to my character that's only use at the moment is to declare my exp point variable "playerExp":
var playerExp : int;
function Start () {
playerExp = 0;
}
I am trying to reference that variable in another script that is connected to my enemy:
var player : GameObject;
var playerExp = player.GetComponent("LevelingScript");
var canReach : boolean;
function Start () {
player = GameObject.Find("Player");
print("Found the player.");
playerExp = player.GetComponent(LevelingScript);
print("Loaded the player's scripts.");
canReach = false;
//*****STATS*****//
var HP : int = 3; //Health Points
var AGL : int = 2; //Agility (miss/dodge)
var ATK : int = 1; //Attack Damage Modifier
}
function OnTriggerEnter () {
canReach = true;
print("Player in range of enemy Scrapper");
}
function OnTriggerExit () {
canReach = false;
print("Player out of range of enemy Scrapper");
}
function Update () {
if(canReach)
if(Input.GetButtonDown('fire1')){
playerExp.playerExp = playerExp.playerExp + 1;
Destroy(this);
}
}
But I get an error: BCE0019: 'playerExp' is not a member of 'UnityEngine.GetComponent'.
Any ideas as to where I went wrong? I would assume the problem lies somewhere in the second script but I could be wrong. Thank you :)
Comment
Best Answer
Answer by Jeric-Miana · Aug 24, 2014 at 12:18 PM
Bro this is the another way to do that, hope this codes will help you
var player : GameObject;
function Start () {
player=GameObject.Find("Player");
}
function OnTriggerEnter(other : Collider)
{
if(other.tag=="Player")
{
player.GetComponent.<getsome>().playerExp+=1;
print("Trigger player");
Destroy(gameObject);
}
}