Home > Mobile >  Detecting variables outside method
Detecting variables outside method

Time:02-25

I was trying to make a 2d Unity Latin translator, but i got a problem. How could i make that it recognises the Text and Dropdown variables already inside the "Declinatio". I need to check for the last 2 characters of this string which is SGenTextStr, and the data should be taken from the SGenText InputField. I dont know if you got a fix for this, i didnt find anything that was helpful on the internet.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class LatinDecl : MonoBehaviour
{
    public static void Declinatio()
    {
        string SGenTextStr;
        string AnsTextStr;
        int StrLength;
        SGenTextStr = SGenText.text;
        AnsTextStr = AnsText.text;
        StrLength = SGenTextStr.Length();
        switch (SGenTextStr.charAt(StrLength - 2)   SGenTextStr.charAt(StrLength - 1))
        {
            default:
                break;
        }
    }
    // Start is called before the first frame update
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
        InputField SGenText;
        Dropdown DeclDrop;
        InputField AnsText;
    }
}

CodePudding user response:

One of the things you may use a lot in Unity games is the Gameobject.GetComponent<>() method. It allows you to access values and functions from other components or scripts in your Unity Scene.

There are also an issue with declaring your variables in Update() as it will cause them to be wiped clean every frame, instead you may want to move them outside all together.

Because of that you will also have trouble with Declinatio() as it is currently static, which wont allow it to reference any non-static variables outside of it.

The last issue is that C# doesn't actually have an implementation of String.charat() however you can use String[int] to achieve the same thing.

With all those changes it would look something like this:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class LatinDecl : MonoBehaviour
{
    public InputField SGenText;
    public Dropdown DeclDrop;
    public InputField AnsText;

    public void Declinatio()
    {
        string SGenTextStr;
        string AnsTextStr;
        int StrLength;
        SGenTextStr = SGenText.GetComponent<InputField>().text;
        AnsTextStr = AnsText.GetComponent<InputField>().text;
        StrLength = SGenTextStr.Length;
        switch (SGenTextStr[(StrLength - 2)]   SGenTextStr[(StrLength - 1)])
        {
            default:
                break;
        }
    }
    // Start is called before the first frame update
    void Start()
    {

    }
    // Update is called once per frame
    void Update()
    {
        
    }
}
  • Related