NullReferenceException when assigning a parent to an instatiated object.

The game level is made of instatiated tiles and I need to make them a child of a single parent LevelDataParent but I always get a null reference exception.

void Start(){
		position = new Vector2 (0, 0);
		for (float i = 0; i<ySize; i++){
			for (float j = 0; j<xSize; j++){
				position.x = j;
				position.y = i;
				GameObject clonedTile = Instantiate(tilePrefab[Random.Range(0,tilePrefab.Length)], CartToIso(position), Quaternion.identity) as GameObject;
				clonedTile.transform.parent = GameObject.Find("LevelDataParent").transform; //Exception is raised here
			}
		}
	}

Thank you in advance.

Is LevelDataParent the name of a GameObject, or a script?

GameObject.Find Finds a game object by name.

GameObject.FindObjectOfType Returns the first active loaded object of the Type.

Try this out:

clonedTile.transform.parent = GameObject.FindObjectOfType(typeof(LevelDataParent)).transform;