Alpha1 Input Issue

Hi All, I ran into a little issue with input working on the text adventure game. I had set up for the ManageState function to expect input for several number keys, as was explained in the tutorial but started seeing this error in the console when I ran the game:

It looked like it was not expecting that kind of input for some reason, so I followed what I saw on the error and set up some Alpha inputs in the input manager:

Anyone know why I ran into this issue?

Hi Tyler, welcome to the community. :slight_smile:

Without seeing your code it would be a guess, but I would imagine you are perhaps passing Alpha1 as a string, so you are trying to acess the names of the Input axes instead of a KeyCode.

Cooy/paste your script into a reply, apply the code formatting and let’s rake a look.


See also;

Hi Rob, thanks for your response. So I am passing Alpha1 as a string. The reason I’m doing so is that when I tried not casting it to a string it was giving me the red squiggly error line under that value getting passed to the Input.GetButtonDown function:

So I ended up passing it as a string like this:

Here is my code:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AdventureGame : MonoBehaviour {

    [SerializeField] Text textComponent;
    [SerializeField] State startingState;

    //int[] oddNumbers = { 1, 3, 5, 7, 9 };
    //string[] daysOfTheWeek = { "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" };
    
    State state;

	void Start () {
        state = startingState;
        textComponent.text = state.GetStateStory();
        //Debug.Log(oddNumbers[3]);
        //Debug.Log(daysOfTheWeek[4]);
	}
	
	// Update is called once per frame
	void Update () {
        ManageState();
	}

    private void ManageState()
    {
        var nextStates = state.GetNextStates();
        Debug.Log(nextStates.Length);
        
        if (Input.GetButtonDown(KeyCode.Alpha1.ToString()))
        {
            Debug.Log(KeyCode.Alpha1.ToString());
        }
        if (Input.GetButtonDown(KeyCode.Alpha1.ToString()) && (nextStates.Length >= 1))
        {
            state = nextStates[0];
        }
        else if (Input.GetButtonDown(KeyCode.Alpha2.ToString()) && (nextStates.Length >= 2))
        {
            state = nextStates[1];
        }
        else if (Input.GetButtonDown(KeyCode.Alpha3.ToString()) && (nextStates.Length >= 3))
        {
            state = nextStates[2];
        }
        else if (Input.GetButtonDown(KeyCode.Alpha4.ToString()) && (nextStates.Length >= 4))
        {
            state = nextStates[3];
        }
        textComponent.text = state.GetStateStory();
    }
}

Hi Tyler,

The issue here is that you are using GetButtonDown instead of GetKeyDown, so the KeyCode values don’t correspond with the predefined virtual buttons.

Update your code to use GetKeyCode and you can then pass in the KeyCode values and all should be well.

Hope this helps :slight_smile:


See also;

1 Like

Hi Rob, thanks! That sorted out the issue!

1 Like

You’re very welcome, glad you can move forward again :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms