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

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.

canControl isn’t a static method. You need an actual instance of a PlatformController to use that function.