0

can't understand what's the problem in there. can you help me please? new to unity and c#, prev learned cpp but now have to learn c#. don't see a problem there. Need some help, please

`

private Text screenText;
private System.Random random = new System.Random();

protected bool pressed;

private void Start(){
    screenText.text = (random.Next(0,10)).ToString();
    pressed = false;
}

private void Update()
{
    Console.WriteLine("started");
    if(Input.GetKey(screenText.text)){
        pressed = true;
        if(pressed){
            generateNewText();
        }
    }
}
AvS
  • 1
  • 2
  • The answer in the [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/q/4660142/1260204) is pretty much a "how to troubleshoot a NRE", a guidebook if you will. Use the advice in that answer to figure out what is causing the NRE in your code – Igor May 06 '22 at 19:10
  • `private Text screenText;` then `screenText.text` guessing you didn't initialize that....but also What is Text as it doesn't relate to System.Text. Looks like a Textbox so shouldn't that be `private TextBox screenText;` but you still need to initialize it..... – Sorceri May 06 '22 at 19:16
  • Going the other way now, as a longtime C# user learning C++, in C# the default value for a class is null. If you do `MyClass myClass` in C++ you get a default initialized instance. If you do it in C# you get null. – Chuck May 07 '22 at 01:50

0 Answers0