Search Unity

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehavio

Discussion in 'Scripting' started by Mike4, Aug 23, 2014.

  1. Mike4

    Mike4

    Joined:
    Oct 24, 2013
    Posts:
    92
    Hi
    You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all

    the line to rewrite is:
    mySurface = new BSplineSurface();

    how to rewrite? Many thanks
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    as the error says, use AddComponent:

    Code (CSharp):
    1.  
    2. mySurface = gameObject.AddComponent <BSplineSurface> ();
    3.  
     
    spraycanmansam likes this.
  3. Mike4

    Mike4

    Joined:
    Oct 24, 2013
    Posts:
    92
    Many thanks, what about this line. I get an error about int
    // int[] triangles = new int[(height - 1) * (width - 1) * 6];
    int[] triangles = gameObject.AddComponent(int[(height - 1) * (width - 1) * 6]);
     
  4. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
  5. Mike4

    Mike4

    Joined:
    Oct 24, 2013
    Posts:
    92
    If I try it the other way round I get:
    The class named 'BSplineSurface' is not derived from MonoBehaviour or ScriptableObject!

    Thanks
     
    Last edited: Aug 23, 2014