Pressing numbers are not working

  1. I am trying to get the input with numbers, but it’s not happening when it’s working if I give any other keywords.

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[] weekDays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };


State CurrentState;
State Stage;

// Start is called before the first frame update
void Start()
{

    CurrentState = StartingState;

    textComponent.text = CurrentState.GetStateStory();

    Debug.Log(oddNumbers[4]);
}

// Update is called once per frame
void Update()
{
    ManageStates();
    if (Input.GetKeyDown(KeyCode.T))
    {
        Debug.Log(weekDays[2]);
    }
}

private void ManageStates()
{
    var nextStates = CurrentState.GetNextStates();

    if (Input.GetKeyDown(KeyCode.A))
    {
        CurrentState = nextStates[0];
    }
    else if (Input.GetKeyDown(KeyCode.S))
    {
        CurrentState = nextStates[1];
    }
    else if (Input.GetKeyDown(KeyCode.Alpha3))
    {
        CurrentState = nextStates[2];
    }
    textComponent.text = CurrentState.GetStateStory();
}

}

State.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = “State”)]
public class State : ScriptableObject
{

[TextArea (14,14)] [SerializeField] string storyText;
[SerializeField] State nextStates;

public string GetStateStory()
{

    return storyText;
}

public State[] GetNextStates()
{
    return nextStates;
}

}

  1. I’m getting this error continuously.

Does the error pop up constantly or only when you press buttons? Is your startingState hooked up in the inspector?

Errors come only if I pressed more than one buttons.
Yes I have already rechecked that and the scripts also.

It seems to be an out of bounds error so the array you’re trying to access must be valid for example if you only have two choices and you press the third button it’s going to try and access a variable that doesn’t exist. Hope that helps

1 Like

Okk I got it. But help me with my first problem. Why number inputs not working ?

Hi Sibam,

I’m sure the problems depend on one another. Fix what @Riley_Waldo suggested first.

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.

1 Like

Are your inputs supposed to be 1, 2 and 3? It looks like it’s A, S and 3

It’s like 1,2,3 only. I tried this many times. But, it’s not working while it’s working in case of A,B,C

As @Riley_Waldo stated: There are no “B” and “C” in your ManageStates method, only A, S and 3.

That’s because 1.2,3 did not work. So I tried with A & S. Then A & S worked but 3 did not.

What happens when you press “3”? Add a Debug.Log to that if-block to see whether a message gets logged into your console when 3 is pressed.

And press the correct “3” key. Alpha refers to the horizontal line, not to the numpad.

Can we see your starting state?

I don’t think the error is with the button checks, it’s what happens after you press a button, once that’s fixed everything will start working.

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 weekDays = { “Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday” };

State CurrentState;
State Stage;

// Start is called before the first frame update
void Start()
{

    CurrentState = StartingState;

    textComponent.text = CurrentState.GetStateStory();

   // Debug.Log(oddNumbers[4]);
}

// Update is called once per frame
void Update()
{
    ManageStates();
  /*  if (Input.GetKeyDown(KeyCode.T))
    {
        Debug.Log(weekDays[2]);
    }
  */
  
}

private void ManageStates()
{
    var nextStates = CurrentState.GetNextStates();

    for (int index = 0; index < nextStates.Length; index++)
    {
        if (Input.GetKeyDown(KeyCode.Alpha1 + index))
        {
            CurrentState = nextStates[index];
        }
    }

/* if (Input.GetKeyDown(KeyCode.A))
{
CurrentState = nextStates[0];
}
else if (Input.GetKeyDown(KeyCode.B))
{
CurrentState = nextStates[1];
}
else if (Input.GetKeyDown(KeyCode.C))
{
CurrentState = nextStates[2];
}
*/
textComponent.text = CurrentState.GetStateStory();
}

}

Here’s the updated one. But still numbers are not working

Okay so your introduction only has one option so it should work if you press A but it you hit B or C it’s trying to access stuff that doesn’t exist. If I remember correctly this problem is fixed later in the lectures but if you want a quick fix now you can just check for null.

Example: first add this method

private void CheckState(int x)
    {
        if (nextStates[x] != null)
            currentState = nextStates[x];
    }

now you can simply change the code to this:

 if (Input.GetKeyDown(KeyCode.A))
{
      CheckState(0);
}
else if (Input.GetKeyDown(KeyCode.B))
{
      CheckState(1);
}
else if (Input.GetKeyDown(KeyCode.C))
{
      CheckState(2);
}

No no. My problem is when I give a input for number it’s not working.

private void ManageStates()
{
var nextStates = CurrentState.GetNextStates();

for (int index = 0; index < nextStates.Length; index++)
{
    if (Input.GetKeyDown(KeyCode.Alpha1 + index))
    {
        CurrentState = nextStates[index];
    }
}

Which key on your keyboard do you press? Where exactly is it located?

Right side. Numeric keyboard. After you asked me this, I tried with the upper side number section and it worked. Then why the right side number section is not working ?

As aforementioned, the Alpha keys refer to the horizontal number keys, not to the numpad. If you want to be able to use the numpad as well, do the following:

if (Input.GetKeyDown(KeyCode.Alpha1 + index) || Input.GetKeyDown(KeyCode.Keypad1 + index))
{
    // code
}

See also:

Privacy & Terms