An object reference is required to access non-static member.

Help Erro: Assets/MFP/Content/Scenes/letreroRota.cs(12,49): error CS0120: An object reference is required to access non-static member `Ligacaoletrero.inputField’

script:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class letreroRota : MonoBehaviour {

    public static Ligacaoletrero inputField;
    public Text myText;
    public Text myText1;


    public void ButtonClick(){
        myText.text="" + Ligacaoletrero.inputField.text+ "";
        myText1.text="" + Ligacaoletrero.inputField.text+ "";
    }
}

I don’t really understand what did you try to achieve with this script.
If “Ligacaoletrero” is type (name of class) of inputField variable, then you don’t need to add it before inputField in lines 12 and 13.
So, the ButtonClick function should look like this:

public void ButtonClick(){
        myText.text="" + inputField.text+ "";
        myText1.text="" + inputField.text+ "";
}

If I’m wrong and you actually wanted to add the inputField variable to class Ligacoletrero or whatever else, then reply with better description of your program and Ligacaoletrero class’s file content.
Also don’t use your native language in programming, you can even use a translation website and just copy/paste the words.
It just makes your code unreadable for others and will certainly drag you down at some point.

1 Like

If “Ligacaoletrero” is a class, with your code, inputField must be defined as a static variable within the “Ligacaoletrero” class. Otherwise, an instance of the “Ligacaoletrero” class must be defined. This is the basis of the “An object reference is required to access non-static member …”

Only static variables can be accessed from a class without it being instanced. However, a static variable probably isn’t what you want, since static variables are equal throughout all instances of the class. In this case, you’re going to want to use

public static Ligacaoletrero inputField = new Ligacaoletrero();

And set it’s text value either via a constructor or through a setter method. Then 1Piotrek1’s code will work for you