Referencing error when finding game objects (c#)

This Script comes up with an error and I’m not certain what it wants me to do.

the script

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class gameOver : MonoBehaviour {

	private GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
	private int numPlayers = players.Length;
	public Text gameOverText;

	void Update()
	{
		if (numPlayers < 1) 
		{
			gameOverText.Text = "GAME OVER";
		}
	}

}

and the error

Assets/gameOver.cs(8,34): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `gameOver.players'

I’ve tried using both public and private variables but neither worked whats going wrong?

You cant use non static field, method or property as a field initializer.

What it means is that when you initialize a field like this it actually is initialize in a static way.

private int numPlayers = players.Length;

is called only once at the creation of the class, and never again. so it shouldn’t depend on an instance.

Just initialize your values in your Start or Awake function and that should fix it.

gameOverText.Text = “GAME OVER”;

you used a capital. ; ) (text)

gameOverText.text = “GAME OVER”;