Search Unity

An object reference is required for the non-static field, method, or property...

Discussion in '2D' started by meikopfaffmann03, Nov 24, 2019.

  1. meikopfaffmann03

    meikopfaffmann03

    Joined:
    Nov 14, 2019
    Posts:
    8
    Hello, I am very new to Unity and C# and wanted to ask for help, here on the forums.
    My Script should show the variable inside the GameObject "Hexagon" called "randomnumber1" inside the console, but it gives me this error:
    Assets\Scripts\GameController.cs(22,19): error CS0120: An object reference is required for the non-static field, method, or property 'Enemy_Hexagon.randomnumber1'

    Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameController : MonoBehaviour
    6. {
    7.  
    8.     void Start()
    9.     {
    10.         if(GameObject.Find("Hexagon") != null)
    11.         {
    12.             Debug.Log("Hexagon exists!");
    13.             GameObject.Find("Hexagon").GetComponent<Enemy_Hexagon>();
    14.         }
    15.         else
    16.         {
    17.             Debug.Log("Hexagon doesn't exist!");
    18.         }
    19.     }
    20.     void Update()
    21.     {
    22.         Debug.Log(Enemy_Hexagon.randomnumber1);
    23.     }
    24. }
    Can someone help me fix this code?
    Thank you all in advance.