Search Unity

Adding Component causes Runtime Error

Discussion in 'Scripting' started by gretty, Jun 26, 2013.

  1. gretty

    gretty

    Joined:
    Jun 20, 2012
    Posts:
    72
    Hello

    I am attempting to instantiate a object but I am getting a runtime error:
    The line of code causing the error is the 2nd line:
    Code (csharp):
    1. GameObject newPlane = EditorUtility.CreateGameObjectWithHideFlags("GooglePlane", 0);
    2. TerrainManager.GooglePlane p = newPlane.gameObject.AddComponent<GooglePlane>();  // ERROR OCCURS HERE
    3. p.transform.parent = gameObject.transform; // NOTE p = null because of error
    I believe the error is that I am attempting to add a component that doesn't exist - "GooglePlane". I think its because "GooglePlane" is part of another class "TerrianManager" so its name is different, something like "TerrainManager.GooglePlane" (although that doesn't work either).

    Any ideas on whats causing this error and how I can fix it?

    Heres my code that implements the GooglePlane class:
    Code (csharp):
    1.  
    2. // In the file Plane.cs
    3. public partial class TerrainManager {
    4.    
    5. public class Plane : MonoBehaviour
    6. {
    7.     // Class Enums  Structs //
    8.     public enum Orientation
    9.     {
    10.         Horizontal,
    11.         Vertical
    12.     }
    13.  
    14.     ....
    15. }
    16. }
    17.  
    18. // In the file GooglePlane.cs
    19. public partial class TerrainManager {
    20.    
    21. public class GooglePlane : TerrainManager.Plane {
    22.    
    23.     void Start () {
    24.    
    25.     }
    26.  
    27.     ....
    28. }
    29. }
     
    Last edited: Jun 26, 2013
  2. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    The main class has to be a MonoBehaviour to be put on a component

    No idea what you are trying to do, but this approach seems wrong.

    Maybe you are trying to do inheritance, ie a base class (TerrainManager) and derived classes, but not sure.
     
    Last edited: Jun 26, 2013
  3. gretty

    gretty

    Joined:
    Jun 20, 2012
    Posts:
    72
    Thanks for your reply :)

    I am trying to use the class TerrainManager as a namespace (from C++). And the classes Plane and GooglePlane are part of this namespace. Finally GooglePlane inherits from Plane. Eventually there will be other classes that inherit from Plane such as BingPlane, ApplePlane and etc.

    Plane inherits from MonoBehaviour (therefore GooglePlane also inherits from this) so it should be valid code shouldn't it? Or am I running into the damn C# multiple inheritance issue?
     
  4. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    Unity needs the main class to be the same name as the file, and derived from MonoBehaviour (at some point). I am also not sure unity handles partial classes either. Why do you need partial classes?
     
    Last edited: Jun 26, 2013
  5. cdevl

    cdevl

    Joined:
    Apr 10, 2013
    Posts:
    180
    I bet Unity cannot handle Monobehaviour that is defined as an inner class.

    Also, I agree with EliteMossy, unless you have a VERY good reason to use partial classes - don't. It's one of these features that a disciplined programmer would touch extremely rarely. Why would I want to split my class into multiple files? Some people think it helps to have multiple team members work on the same code but modern source control tools make this idea obsolete.

    I also think you don't need to emulate namespaces. Why not just use a namespace. It used to be a problem for Monobehaviours before Unity v4. I think it works now (haven't tried it myself though).
     
    Last edited: Jun 26, 2013
  6. gretty

    gretty

    Joined:
    Jun 20, 2012
    Posts:
    72
    Yeah thats why I was using Partial Classes, because I couldn't use namespaces in Unity. But I just got Unity4 and it looks like its letting me use namespaces so I'll go down that route.

    The whole reason for this is because the NGUI package also implements the class 'Plane' so I get a ambiguous conflict because it doesn't know whether to create a NGUI Plane or my Plane. The obvious solution was namespaces but prior to V4 this wasn't possible in Unity's C# scripts.
     
  7. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    Makes sense now, yeah Unity 4 supports namespaces afaik