Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ERROR: Object reference is required nonstatic field field, method.,Object reference is required meth

Discussion in 'Scripting' started by Rockysek018, Jan 14, 2020.

  1. Rockysek018

    Rockysek018

    Joined:
    Jan 11, 2019
    Posts:
    13
    Hey,
    so I ran into this problem
    I saw few fixes but nothing worked for me.

    Does anyone know what to do with this?
    Thanks.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class TextInput : MonoBehaviour
    7. {
    8.  
    9.     public InputField inputField;
    10.  
    11.     GameControlller controller;
    12.  
    13.     void Awake()
    14.     {
    15.         controller = GetComponent<GameControlller>();
    16.         inputField.onEndEdit.AddListener(AcceptStringInput);
    17.     }
    18.  
    19.     void AcceptStringInput(string userInput)
    20.     {
    21.         userInput = userInput.ToLower();
    22.         controller.LogStringWithReturn(userInput);
    23.  
    24.         char[] delimiterCharacters = { ' ' };
    25.         string[] seperateInputWords = userInput.Split(delimiterCharacters);
    26.  
    27.         for (int i = 0; i < controller.inputActions.Length; i++)
    28.         {
    29.             InputAction inputAction = controller.inputActions[i];
    30.             if (inputAction.keyWord == seperateInputWords[0]) ;
    31.             { }
    32.         }
    33.  
    34.         {
    35.  
    36.             InputAction.RespondToInput (controller, seperateInputWords);
    37.  
    38.         }
    39.  
    40.         InputCompete();
    41.     }
    42.  
    43.     void InputCompete()
    44.     {
    45.         controller.DisplayLoggedText();
    46.         inputField.ActivateInputField();
    47.         inputField.text = null;
    48.     }
    49. }
    50.  
     
  2. Deleted User

    Deleted User

    Guest

    Please, post the entire error message otherwise we can only guess what could be the problem.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    You should always post the error message since it gives line numbers, but a quick glance I assume this is your error.

    Code (CSharp):
    1. InputAction.RespondToInput (controller, seperateInputWords);
    You have a capital "I" so it's pointing to the class and not the instance that you you maybe meant to create. However, you also have a lot of extra curly brackets and the instance you did create above it is within a for loop, which means the line I reference would not access the instance.
     
  4. Deleted User

    Deleted User

    Guest

    I suspected this as well.
     
    Last edited by a moderator: Jan 18, 2020
  5. Rockysek018

    Rockysek018

    Joined:
    Jan 11, 2019
    Posts:
    13
    So sorry about that!

    The full error is:
    An object reference is required for non-static field, method, or propety 'InputAction.RespondToInput(GameController,string[])'
     
  6. Rockysek018

    Rockysek018

    Joined:
    Jan 11, 2019
    Posts:
    13
    So sorry about that!

    The full error is:
    An object reference is required for non-static field, method, or propety 'InputAction.RespondToInput(GameController,string[])'
     
  7. Rockysek018

    Rockysek018

    Joined:
    Jan 11, 2019
    Posts:
    13
    Hello,
    I lowered the I in inputAction but now it just says:
    The name 'inputAction' does not exist in the current context
     
  8. Deleted User

    Deleted User

    Guest

    You need to learn more about scripting.

    You need to declare and define inputAction to fix the problem.
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    You need to learn about the scope of variables. inputAction doesn't work there because where you declared it is in a for loop. As I said, you can't access it. When you declare a variable in a for loop, it belongs to that for loop and can't be accessed outside of it.
     
  10. Rockysek018

    Rockysek018

    Joined:
    Jan 11, 2019
    Posts:
    13
    Yeah I fixed it somehow..