- Home /
How would I access a Class Variable from another script?
Hello,
I've searched the forums and answers on this, and have perhaps improved my code but it still doesn't work -- I get "'player1' is not a member of 'gameController'. " as an error on the following scripting. Any idea what I'm doing wrong? I'm guessing the error is in the player script.
gameController.js
class PlayerData{
public var mightBase;
public var mightBonus;
}
var player1 : PlayerData = PlayerData();
player1.mightBase = PlayerPrefs.GetInt(gameNumber + "_1_might_base");
player1.mightBonus = PlayerPrefs.GetInt(gameNumber + "_1_might_bonus");
playerScript.js
playerMight = gameControllerObject.GetComponent(gameController).player1.mightBase + gameControllerObject.GetComponent(gameController).player1.mightBonus;
The name in GetComponent has to be exactly the same as the file name, aka if the file name is Game Controller, the get component has to be "Game Controller"
Just as a standard na$$anonymous$$g convention, name it GameController, not gameController and no space :)
Oh, they are correct. I'm modifying my code from using individual variables to using the class -- first time using the class.
That's why I'm pretty sure the problem is in the player script part, specifically the "player1.mightBase". Usually I'd just have a var player1$$anonymous$$ightBase so calling .player1$$anonymous$$ightBase would work (And did), but I'm not sure how to get access to the class variable.
gameControllerObject.GetComponent(gameController).player1.mightBase
Answer by infinitypbr · Feb 20, 2013 at 05:45 AM
Oops -- just forgot to put : int in there.
public var mightBase : int;
public var mightBonus : int;