- Home /
Accessing components/variables?
Hello. I am trying to use GetComponent to access public variables that are in another script, but I keep getting errors(such as "An instance of type 'UnityEngine.Component' is required to access non static member 'GetComponent'.").
I want to make a pokemon style turn based RPG. I am going to have all of the available moves that the monster can do be functions and store those functions a single Moves_Database script. Then, when a monster goes to execute a move, that move's function would be called, but it would then be modified by the stat values inside that particular monsters script.
he way he battle system works, is first the script scans the scene for all objects with the enemy tag (which all have the "Enemy" script attached), then, targets a specific enemy, then it it accesses that enemy's specific script (the enemy's stats are public variables), and then, for the attack, it enacts some sort of modifier on those stats (subtracting HP, decreasing agility, etc.).
Here is the the one that invokes the other script's function(its called B_Controlled)
public var Monster_List :GameObject[]; public var Monster_SLCT =5; public var Attacks_SLCT : String;
function P1 (){Monster_List=GameObject.FindGameObjectsWithTag ("Enemy");} function P2 (){Monster_SLCT = 10;}
function P3 (){Attacks_SLCT = "THUNDER";} function P4 (){Invoke(A_Moves.Attacks_SLCT);}
And here is the moves database (A_Moves)
static function THUNDER (){ //This is the part of the function that finds the enemy that the B_controlled script found. GetComponent("B_Controlled").Monster_List[GetComponent("B_Controlled").Monster_SLCT]
//and this part affects the stats of the Enemy script .GetComponent("Enemy").HP-=1; }
Just to make the question a little easier to answer, perhaps you could edit your post to include the exact error message, and the specific statement in your code that generates the error message.
Answer by loopyllama · Apr 17, 2011 at 01:36 PM
GetComponent is really Component.GetComponent in your script, where Component is the instance of the class. Your function is static whichs means it cannot access any instances of the class. This is why you get an error message saying you need an instance of Component in order to call GetComponent. Make your function public rather than static, then you can access instances and call the function from other classes.