Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by ErikMartins, Apr 6, 2017.

  1. ErikMartins

    ErikMartins

    Joined:
    Oct 28, 2016
    Posts:
    49
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class letreroRota : MonoBehaviour {
    5.  
    6.     public static Ligacaoletrero inputField;
    7.     public Text myText;
    8.     public Text myText1;
    9.  
    10.  
    11.     public void ButtonClick(){
    12.         myText.text="" + Ligacaoletrero.inputField.text+ "";
    13.         myText1.text="" + Ligacaoletrero.inputField.text+ "";
    14.     }
    15. }
     
  2. 1Piotrek1

    1Piotrek1

    Joined:
    Mar 14, 2014
    Posts:
    130
    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:
    Code (CSharp):
    1. public void ButtonClick(){
    2.         myText.text="" + inputField.text+ "";
    3.         myText1.text="" + inputField.text+ "";
    4. }
    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.
     
    Ericks89 likes this.
  3. huelsman

    huelsman

    Joined:
    Jan 20, 2017
    Posts:
    18
    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