Hello,
Im working on a small side project for fun right now, but with this issue, it isnt fun anymore.
I want to create a console where you can program simple commands like Rotate [angle] and Move.
The problem is, i cant convert my extracted argument from the console to an intager or float.
I’ve tried everything from Parsing to Converting, Casting and i even wrote an loop that loops through all possible values and compares them to the string if one is the right number and returns it, but it just dosnt work with no real reason
i have been reading a lot through other answers that explain i need some Culture info or something but that didn’t work either.
.
Player movement:
public class PlayerController : MonoBehaviour
{
public Animator anim;
public float playerMoveStep = 1.2f;
private void Awake()
{
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
print("Timer Started..");
StartCoroutine("timer");
}
public void Move()
{
transform.position += transform.forward * playerMoveStep;
anim.Play("Player_MOVE");
}
public void RotatePlayer(string sAngle)
{
int angle = int.Parse(sAngle);
transform.rotation = Quaternion.Euler(0, angle, 0);
}
IEnumerator timer()
{
yield return new WaitForSeconds(1f);
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
print("Timer ended");
}
}
Console System:
public class CommandConsole : MonoBehaviour
{
public TMP_InputField console;
public string command;
public string args;
public void RunCode()
{
List<string> lines = new List<string>();
lines = EnumerateLines(console.textComponent);
StartCoroutine("RunLines", lines);
}
List<string> EnumerateLines(TMP_Text text)
{
TMP_TextInfo textInfo = text.GetTextInfo(text.text);
List<string> lines = new List<string>();
for(int i = 0; i < textInfo.lineCount; i++)
{
TMP_LineInfo line = textInfo.lineInfo*;*
lines.Add(text.text.Substring(line.firstCharacterIndex, line.characterCount));
}
return lines;
}
void MovePlayer()
{
PlayerController player = GameObject.FindObjectOfType();
player.GetComponent().Move();
print(“Moving player…”);
}
void RotatePlayer(string angle)
{
PlayerController player = GameObject.FindObjectOfType();
player.GetComponent().RotatePlayer(angle);
print(“Rotating player…”);
}
IEnumerator RunLines(List lines)
{
for (int i = 0; i < lines.Count; i++)
{
string _command = lines*;
_command = _command.Split(’ ')[0];*
command = _command;
if (_command.Contains(“Move”))
{
Debug.Log(_command);
MovePlayer();
}
if (_command.Contains(“Rotate”))
{
args = lines*.Split(’ ')[1];*
Debug.Log(_command);
RotatePlayer(lines*.Split(’ ')[1].ToString());*
}
yield return new WaitForSeconds(0.5f);
}
}
}
I would really appreachiate it, if you could answer this question because i am really struggeling with it.
(I even copied some code from another project where this float parsing works and even that didnt work)