Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Unity has my classname, can i do anything?

Discussion in 'Scripting' started by Nanako, Dec 9, 2014.

  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    I'm trying to make a class called Debug, with various special debugging functions that will be useful to my project.

    well that sure didnt work, it already exists. M>y next logiccal thought then, is to try to extend/inherit from the existing one. But i'm told that debug is a sealed class :(

    must i just select another name? i was really fond of that one.
     
  2. HonorableDaniel

    HonorableDaniel

    Joined:
    Feb 28, 2007
    Posts:
    2,844
    What? It should work to call your class Debug. There is no Debug class in the global namespace, unity's is in the UnityEngine namespace.
     
    Jessy and Kirk Clawson like this.
  3. Kirk Clawson

    Kirk Clawson

    Joined:
    Nov 4, 2014
    Posts:
    65
    You could always place it in your own namespace.
    Code (CSharp):
    1. namespace MyNamespace
    2. {
    3.     public class MyClass
    4.     {
    5.         ...
    6.     }
    7. }
    Then you reference it as MyNamspace.MyClass
     
  4. HonorableDaniel

    HonorableDaniel

    Joined:
    Feb 28, 2007
    Posts:
    2,844
    To add to my initial post, if you are using UnityEngine, and the Debug class is in the global namespace, then there's a good chance of receiving an ambiguous compilation error. To resolve the error, simply do as follows:
    Code (csharp):
    1. global::Debug.Log();
    And to call the unity one
    Code (csharp):
    1. UnityEngine.Debug.Log();
     
    rakkarage likes this.
  5. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    WARNING!
    If you do it this way, be careful not to place using MyNamespace at the top. If you do this and you try to call Debug, the compiler won't know which one you are trying to call. This is a perfect example of why REAL C++ coders NEVER use the directive usingnamespace std; at the top because of this very problem.

    If you do implement your own class that Unity currently has as one of its classes then either remove using UnityEngine or call the namespace directly when you want to call your Debug class via MyNamespace.Debug.
     
    Nanako likes this.
  6. HonorableDaniel

    HonorableDaniel

    Joined:
    Feb 28, 2007
    Posts:
    2,844
    Nah, nothing to worry about. As I stated above, the compiler will warn of the ambiguouse reference. Have you not ever heard of CS0104??

    http://msdn.microsoft.com/en-us/library/e094swa1(v=vs.90).aspx

    Just type out the fully qualified name and you're good to go.
     
  7. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
  8. HonorableDaniel

    HonorableDaniel

    Joined:
    Feb 28, 2007
    Posts:
    2,844
    This is C#, not C++
     
  9. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Same thing.
     
  10. element_wsc

    element_wsc

    Joined:
    Jul 27, 2012
    Posts:
    522
    to add to the suggestions of the other folks. You can also explicitly tell the compiler wich Debug version to use without typing the full qualified name all the time.
    Example

    Code (csharp):
    1. using UnityEngine;
    2. using MyNameSpace;
    3. using Debug = MyNameSpace.Debug;
    4.  
    5. Debug.Log() // now refferes to MyNameSpace.Debug.Log()
     
  11. mweldon

    mweldon

    Joined:
    Apr 19, 2010
    Posts:
    109
    What he means is that there is no need to panic over such a small issue because C# is not a ticking time bomb like C++. The issue can be resolved quite easily and the solution is described in the very link that you posted.