Assigning a text file in unity inspector : NullReferenceException

I’m trying to assign a file in unity inspector.

With Javascript, it was so easy.


#pragma strict

public var xmlFile : TextAsset;

function Start () {

	Debug.Log(xmlFile.text);

}

function Update () {

}

Just dragging the file from project window to inspector, I could see the content of xml file.

But, in C#, a strange exception was occured.


using UnityEngine;
using System.Collections;
using System.Xml;

public class M3 : MonoBehaviour {
	
	public TextAsset OEXmlFile;
	
	public M3()
	{
		Debug.Log(OEXmlFile.text);
	}

}

Error message here(Runtime):

NullReferenceException: Object reference not set to an instance of an object
M3…ctor () (at Assets/Resources/Scripts/M3.cs:11)

[24878-screen+shot+2014-04-08+at+2.29.26+am.png|24878]

I obviously assigned xml file in inspector window.

What’s the problem in C#?
How Can I assign a file on inspector in C#?

You don’t use constructors when writing code that derives from MonoBehaviour. Instead you use an Awake or Start function. Also, in your screenshot the XML is being displayed, so this is not a text asset problem.