GameObject.FindWithTag returns all the time null


Well I’m trying to make a independent class for all scripts for make my game source more organizated. But when I try to get variables values from scripts, FindWithTag function returns a null value that cause a NullReferenceException on my game…

So that it’s the code that I had

For General Classes not indexed to the Inspector

public static void OnStart() 

			PlayerObject = GameObject.FindWithTag("PlayerObject").GetComponent<index>().PlayerObject; //getGameObjects.PlayerObject;
			tileSize = GameObject.FindWithTag("tileSize").GetComponent<index>().tileSize;
			baseLine = GameObject.FindWithTag("baseLine").GetComponent<index>().baseLine;
			seed = GameObject.FindWithTag("seed").GetComponent<index>().seed;
			water = GameObject.FindWithTag("water").GetComponent<index>().water;
			WaterLevel = GameObject.FindWithTag("WaterLevel").GetComponent<index>().WaterLevel;
			Terrain linkedTerrain = GameObject.FindWithTag("myterrain").GetComponent<index>().myterrain;
			_terrainGrid [0,0] = Terrain.CreateTerrainGameObject (linkedTerrain.terrainData).GetComponent<Terrain> ();
			_terrainGrid [0,1] = Terrain.CreateTerrainGameObject (linkedTerrain.terrainData).GetComponent<Terrain> ();
			_terrainGrid [0,2] = Terrain.CreateTerrainGameObject (linkedTerrain.terrainData).GetComponent<Terrain> ();
			_terrainGrid [1,0] = Terrain.CreateTerrainGameObject (linkedTerrain.terrainData).GetComponent<Terrain> ();
			_terrainGrid [1,1] = linkedTerrain;
			_terrainGrid [1,2] = Terrain.CreateTerrainGameObject (linkedTerrain.terrainData).GetComponent<Terrain> ();
			_terrainGrid [2,0] = Terrain.CreateTerrainGameObject (linkedTerrain.terrainData).GetComponent<Terrain> ();
			_terrainGrid [2,1] = Terrain.CreateTerrainGameObject (linkedTerrain.terrainData).GetComponent<Terrain> ();
			_terrainGrid [2,2] = Terrain.CreateTerrainGameObject (linkedTerrain.terrainData).GetComponent<Terrain> ();
			SetPlayerOnGround(_terrainGrid[1,1], PlayerObject);


For script indexed in the Insepctor:

using UnityEngine;
using System.Collections;
using WorldGeneration;

public class index : MonoBehaviour {
	public GameObject PlayerObject;

	public Terrain myterrain;

	public float tileSize = 2f;
	public float baseLine = 0.5f;
	public int seed = 2; //(int)Time.time;
	public WaterSimple water;
	public int WaterLevel = 20;

	// Use this for initialization
	void Start () {


	// Update is called once per frame
	void Update () {



For some reason it doesn’t work, can somebody guide me with this?

Thanks in advance…

If I don’t do that, I had to call all the function with a lot of parameters and I don’t want that… Because I think that it will make slower the game… And it will affect to the perfomance…

And also, I had to call all the functions with a static modifier, if not I had to declare a new instance for every non-shared function & variable, but I don’t know how to do that… (I want do that because I will use an unnique function for call others)

I had solved it with a ugly function:

	public static void SetValues(GameObject PlayerObject2, float tileSize2, float baseLine2, int seed2, WaterSimple water2, int WaterLevel2, Terrain linkedTerrain2) 
			PlayerObject = PlayerObject2;
			tileSize = tileSize2;
			baseLine = baseLine2;
			seed = seed2;
			water = water2;
			WaterLevel = WaterLevel2;
			linkedTerrain = linkedTerrain2;

But I want to know if it possible to make it more improved…

Did you even instantiate these objects on runtime? If not they do not even exist so unity can’t find them.i dont c that u did instantiate any gameobjects.