Canias
Canias

Reputation: 55

Unity C# Variable input

I've been leerning C# recently. the thing is: I have several gameobjects with the same kind of movement whom recieve diferent inputs, are in different layers and are atacked by multiple enemies on those layers, so, to save me some tedious work I tried to use the same movementscript for all of them. I have a script witch estimates diferent texts for strings variables, that later, are used to give a key name on the movement script. It aparently doesn't give me errors while typing it but it doesn't work either, I don't know if what I want to do is possible or if I wrote it wrong. I Went wild, but it sems legit.

Here the InputScript:

public string forward;
public string backwards;
public string hold;
GameObject squadA;
GameObject squadB;
GameObject squadC;
GameObject squadD;

void Start () {

    squadA = GameObject.Find ("Squad A");
    squadB = GameObject.Find ("Squad B");
    squadC = GameObject.Find ("Squad C");
    squadD = GameObject.Find ("Squad D");

    //A Input
    if (gameObject == squadA) {  

        forward = "KeyCode.Alpha3";
        backwards = "KeyCode.Alpha1";
        hold = "KeyCode.Alpha2";
    }

    //B Input
    if (gameObject == squadB) {

        forward = "KeyCode.E";
        backwards = "KeyCode.D";
            hold = "KeyCode.W";
    }

    //C Input
    if (gameObject == squadC) {

            forward = "D";
            backwards = "A";
            hold = "S";
    }

    //D Input
    if (gameObject == squadD) {

            forward = "C";
            backwards = "Z";
            hold = "X";
    }
}

And the movement one (I don't think the second half is necessary but just in case (the movement is weird but it's exacly the one I want)):

public float constantSpeed = 3;
float counterConstantSpeed = -3;
float speed;
float spawnpos;
public bool goingBackwardsOrStatic = true;
public bool holding =false;
KeyInput ScriptBeholderKI;

void Start () {

    spawnpos = transform.position.x;
    ScriptBeholderKI = gameObject.GetComponent <KeyInput> ();
}

//Key inputs

void Update () {


    transform.Translate (constantSpeed * Time.deltaTime, 0, 0);
    if (Input.GetKeyDown (ScriptBeholderKI.forward)) {   

        StopAllCoroutines ();
        StartCoroutine (RightMovement(0f));
    }

    if (Input.GetKeyDown (ScriptBeholderKI.backwards)) {

        StopAllCoroutines ();
        StartCoroutine (LeftMovement(0f));
    }

    if (Input.GetKeyDown (ScriptBeholderKI.hold)) {

        StopAllCoroutines ();
        StartCoroutine (Hold(0f));
    }
}

//Movement itself (Right, Left, hold)

IEnumerator RightMovement (float Rloop) {

    while (transform.position.x < constantSpeed * Time.time + spawnpos + 16) {

        goingBackwardsOrStatic = false;
        speed = 10f;
        transform.Translate (speed * Time.deltaTime, 0, 0);
        yield return new WaitForSeconds (Rloop);
    }

    if (transform.position.x > constantSpeed * Time.time + spawnpos + 15.9) {

        StopAllCoroutines ();
        StartCoroutine (LeftMovement (0f));
    }
}

IEnumerator LeftMovement (float Lloop) {

    goingBackwardsOrStatic = true;
    while (transform.position.x > constantSpeed * Time.time + spawnpos) {

        speed = -7f;
        transform.Translate (speed * Time.deltaTime, 0, 0);
        yield return new WaitForSeconds (Lloop);
    }
}

IEnumerator Hold (float Lloop) {

    holding = true;
    while (transform.position.x > constantSpeed *Time.time + spawnpos) {

        transform.Translate (counterConstantSpeed * Time.deltaTime, 0, 0);
        yield return new WaitForSeconds (Lloop);
    }
}

this gives me the following error later on the player:

ArgumentException: Input Key named: KeyCode.Alpha3 is unknown UnityEngine.Input.GetKeyDown (System.String name) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineInputBindings.gen.cs:533) ASquadMovement.Update () (at Assets/Scripts/Squad/Self behavoiurs/Squads movement/ASquadMovement.cs:26)

Upvotes: 1

Views: 1266

Answers (1)

Programmer
Programmer

Reputation: 125275

You are comapring "KeyCode.Alpha3" and other values which doesn't exist. You can do this with enum or string but I think that enum is appropriate for this because it makes sure that the KeyCode exist.

I in your KeyInput class, change

public string forward;
public string backwards;
public string hold;

to

public KeyCode forward;
public KeyCode backwards;
public KeyCode hold;

Then replace your whole if statement with the code below:

//A Input
if (gameObject == squadA)
{

    forward = KeyCode.Alpha3;
    backwards = KeyCode.Alpha1;
    hold = KeyCode.Alpha2;
}

//B Input
if (gameObject == squadB)
{

    forward = KeyCode.E;
    backwards = KeyCode.D;
    hold = KeyCode.W;
}

//C Input
if (gameObject == squadC)
{

    forward = KeyCode.D;
    backwards = KeyCode.A;
    hold = KeyCode.S;
}

//D Input
if (gameObject == squadD)
{

    forward = KeyCode.C;
    backwards = KeyCode.Z;
    hold = KeyCode.X;
}

You need to learn C# before using Unity or you will make simple mistakes like this. Here is an enum tutorial.

Upvotes: 1

Related Questions