Alternative New() - Monobehaviour

Someone would have there a solution to replace the “new” that can not operate using a Monobehaviour (nullref)?

public class GlobalMembersAw_Display : MonoBehaviour
    CGame bla = new CGame();

    CAwInterface CawInstance = new CAwInterface();

Thanks you

I'm not sure what you mean. Do you try to create a class with new that is derived from MOnoBehaviour? That's not possible since a MonoBehaviour is a Component. Components can only exist on GameObject instances. The only way to create them is using AddComponent or to specify it when creating the GameObject.

// C#
CMyMonoBehaviour bla = myGameObject.AddComponent<CMyMonoBehaviour>();

// or
CMyMonoBehaviour bla = (CMyMonoBehaviour)myGameObject.AddComponent(typeof(CMyMonoBehaviour));

// or
GameObject myGameObject = new GameObject("name",typeof(CMyMonoBehaviour));
CMyMonoBehaviour bla = myGameObject.GetComponent<CMyMonoBehaviour>();

// or (if you're not interested in the gameObject instance)
CMyMonoBehaviour bla = (new GameObject("name")).AddComponent<CMyMonoBehaviour>();

For those who use UnityScript (JavaScript):

// UnityScript
var bla = myGameObject.AddComponent.<CMyMonoBehaviour>();

// or
var bla : CMyMonoBehaviour = myGameObject.AddComponent(CMyMonoBehaviour);

// or
var myGameObject = new GameObject("name",CMyMonoBehaviour);
var bla = myGameObject.GetComponent.<CMyMonoBehaviour>();

// or (if you're not interested in the gameObject instance)
// ! I'm not sure if that works !
var bla = (new GameObject("name")).AddComponent.<CMyMonoBehaviour>();