- Home /
Create an Instance?
How do I create an instance? I'm struggling with this script and now I get this error.
An instance of type 'PlatformerController' is required to access nonstatic member 'canControl'
Here is the script:
private var cameraScrolling : CameraScrolling;
private var selected = 0;
static var player1 : PlatformerController;
static var player2 : PlatformerController;
var targets : Transform[];
function Awake () {
cameraScrolling = GetComponent (CameraScrolling);
player1.GetComponent(PlatformerController);
player2.GetComponent(PlatformerController);
cameraScrolling.SetTarget (targets[0], true);
}
function Update () {
if (Input.GetKeyUp(KeyCode.Alpha2))
PlatformerController.canControl (player2, true);
PlatformerController.canControl (player1, false);
cameraScrolling.SetTarget (targets[1], true);
}
function LateUpdate () {
if (Input.GetKeyUp(KeyCode.Alpha1))
PlatformerController.canControl (player1, true);
PlatformerController.canControl (player2, false);
cameraScrolling.SetTarget (targets[0], true);
}
Please help
Answer by Bunny83 · May 28, 2011 at 12:19 AM
Some notes on your script:
PlatformerController is a class so if you use this name and put a dot behind it you access the class, not a specific instance of that class.
You can access only static members if you use the classname.
In Awake you use your two variables (player1 & player2) to call GetComponent on the gameobject these scripts are attached, but you never set any values to these variables.
GetComponent returns the instance of the class that you specify inside the brackets if there is one on the given GameObject. You do nothing with the returned value so the whole call to GetComponent is useless.
Your if statements in Update/LateUpdate do only affect the next command because there are no curly-brackets, but it seems that you want both or maybe all three lines in the if block.
I don't see the reason why one "if" is in Update and the other in LateUpdate
We don't know how your PlatformerController script looks like but it seems the function "canControl" belongs to an instance(non static) and not to the class(static).
I'm not sure what canControl
actually do but i guess it can enable the control for another player so your script should look like this:
private var cameraScrolling : CameraScrolling;
private var selected = 0;
static var player1 : PlatformerController;
static var player2 : PlatformerController;
var targets : Transform[];
function Awake () {
cameraScrolling = GetComponent (CameraScrolling);
cameraScrolling.SetTarget (targets[0], true);
}
function Update () {
if (Input.GetKeyUp(KeyCode.Alpha2)) {
player1.canControl (player2, true);
player2.canControl (player1, false);
cameraScrolling.SetTarget (targets[1], true);
}
if (Input.GetKeyUp(KeyCode.Alpha1)) {
player1.canControl (player2, false);
player2.canControl (player1, true);
cameraScrolling.SetTarget (targets[0], true);
}
}
I don't know where your player1 and player2 are initialized but they have to point to your PlatformerController-instances that are attached to your players. Is there any good reason why they are static? static members can't be assigned in the inspector.
Thank you for a very detailed answer,the answer to all your why did you do this, is simply because I am a beginner and a noob :)
What I actually want is to enable or disable a var in 'PlatformerController' called 'canControl' throught this script. 'Platformer controller is a script placed on my players. canControl just enables the control for that player. The real problem is that I can choose between my two players, but the controls are enabled for both, so they both respond when I move them around...
Is this PlatformerController a script of yours? I remember there's a canControl variable in the Character$$anonymous$$otor script - can this be the one you want?
If you were to access the canControl variable in the Character$$anonymous$$otor.js script, you should do something like this:
var script1: Character$$anonymous$$otor = transform.GetComponent(Character$$anonymous$$otor); script1.canControl = true;
If your script is really PlatformerController.js, just change the Character$$anonymous$$otor to PlatformerController
Answer by flaviusxvii · May 27, 2011 at 10:24 PM
canControl isn't a static method. You need an actual instance of a PlatformController to use that function.
Yes I figured it was something like that, I am just perplexed on how to do this. How do I instance PlatformerController?