0

Image Here I'm trying to do this thing where I type a word into an inputfield, where it's encrypted into L337 casing, and the result is outputted in the text object. But I keep getting the error Object Reference Not set to an Instance of an Object and it points to

inputText = input.text;
 output.text = inputText;

I'm still a bit of a noob. If someone could help me out, I'd appreciate it.

public class Encryption : MonoBehaviour
{
public InputField input;
public Text output;
string inputText;

public Toggle L337Toggle;

 void Start()
{
    output = GetComponent<Text>();
    input = GetComponent<InputField>();

    L337Toggle.isOn = false;

}
private void Update()
{
   inputText = input.text;
    output.text = inputText;

    var textEncryption = new TextEncryption(inputText);
    var L337Encryption = new L337Encryption(textEncryption);

    if (Input.GetKeyDown("enter"))
    {
        if (L337Toggle.isOn == true)
        {
            string result = L337Encryption.encrypt();
        }
    }
}


public interface IEncryption
{
    string encrypt();
}

public class TextEncryption : IEncryption
{
    private string originalString;

    public TextEncryption(string original)
    {
        originalString = original;
    }
    public string encrypt()
    {
        Debug.Log("Encrypting Text");
        return originalString;
    }
}

public class L337Encryption : IEncryption
{
    private IEncryption _encryption;

    public L337Encryption(IEncryption encryption)
    {
        _encryption = encryption;
    }
    public string encrypt()
    {
        Debug.Log("Encrypting L337 Text");
        string result = _encryption.encrypt();
        result = result.Replace('a', '4').Replace('b', '8').Replace('e', '3').Replace('g', '6').Replace('h', '4').Replace('l', '1')
            .Replace('0', '0').Replace('q', '9').Replace('s', '5').Replace('t', '7');

        return result;
    }
}

}

Kenneth Dionisi
  • 59
  • 1
  • 3
  • 8
  • `output = GetComponent();` and `input = GetComponent();` means that the Text and InputField components are supposed to be attached to the GameObject your `Encryption` script is attached to. If not then Find the GameObjects that have the Text and InputField components before then call `GetComponent();` on them. – Programmer Oct 18 '17 at 22:41
  • I made an empty game object called encryptManager, and coupled the input field and output text onto the variables in the script component. I dont' see what I did wrong. – Kenneth Dionisi Oct 18 '17 at 22:48
  • Maybe you should post screenshot of the Hierarchy tab that shows where the Text and InputField – Programmer Oct 18 '17 at 23:05
  • Ok. I just did. – Kenneth Dionisi Oct 18 '17 at 23:19
  • If you assigned it from the EDitor then remove these line `output = GetComponent();` and `input = GetComponent();` – Programmer Oct 18 '17 at 23:21

0 Answers0