- Home /
This question was
closed 6 days ago by
gjf.
Question by
AbdulhamidB7 · 6 days ago ·
c#stringinputfieldint
Convert the user's input to an integer if it is equal to a key string from a different class.
So I need to convert the player's input to the input field into an integer if it is equal to a key string. I've attempted it but it is not behaving as I would want it to. I have a debug.Log statement but the console doesn't print it until after play mode is exited. I also noticed I can't seem to call from the Action script properly. Here's my attempt, what am I doing wrong?
using UnityEngine;
using UnityEngine.UI;
public class TextInput : MonoBehaviour
{
public InputField inputField;
GameController controller;
public Action actions;
void Awake()
{
controller = GetComponent<GameController>();
inputField.onEndEdit.AddListener(AcceptStringInput);
}
public void AcceptStringInput(string userInput)
{
userInput = userInput.ToLower();
controller.LogStringWithReturn(userInput);
if (userInput == actions.keyString)
{
int.TryParse(userInput, out int result);
Debug.Log("You entered a number");
}
InputComplete();
}
void InputComplete()
{
controller.DisplayLoggedText();
inputField.ActivateInputField();
inputField.text = null;
}
}
Here's the action class as well.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Action
{
public string keyString;
public string actionDescription;
[TextArea]
public string outcome;
public Event nextEvent;
}
Comment
Follow this Question
Related Questions
Converting a string to an int 2 Answers
string + int = variable 2 Answers
How to reference a variable in another script based on string in c# 0 Answers
Convert Text to float 3 Answers
Multiple Cars not working 1 Answer