Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Article: "Methods of organizing the interaction between scripts in Unity"

Discussion in 'General Discussion' started by nubick, Mar 19, 2015.

  1. nubick

    nubick

    Joined:
    Nov 17, 2012
    Posts:
    30
    Not_Sure and JasonBricco like this.
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I suggest using code examples that follow convention and will compile

    Code (CSharp):
    1. public ScoresManager ScoresManager;
    There are plenty of other methods of interaction that at least deserve a mention. No article about script interaction is complete without a nod to GetComponent, SendMessage and ExecuteEvent
     
  3. nubick

    nubick

    Joined:
    Nov 17, 2012
    Posts:
    30
    What convention do you mean?

    Could you give me more details about "ExecuteEvent" class or method? I don't know it.
    List all possible methods was not goal of article, I wanted to tell about EventAggregator approach at first.
     
  4. vladk

    vladk

    Joined:
    Jul 10, 2008
    Posts:
    167
    Honestly? You've just invented a bicycle. There is a book called "Design patterns: Elements of reusable object oriented software" and it holds everything you ever need when hitting a problem of designing "a better architecture" for your code.

    But it's good you actually moving in this direction, cause you have no idea how many people prefer to write a code with their ass instead of brain :)
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I was referring to case conventions around naming classes, methods and variables. The line I quoted will not compile, as the variable name is the same as the type name.

    http://docs.unity3d.com/ScriptReference/EventSystems.ExecuteEvents.html
     
  6. Jither

    Jither

    Joined:
    Jun 13, 2014
    Posts:
    29
    There are no official naming conventions for public instance fields, since you're not supposed to have public instance fields in the first place. That said, if you're using them anyway, the closest you'll get to a naming convention (based on the conventions for public static fields and public properties) is... PascalCase - exactly what the OP is using. Guess what? It will compile fine too. The C# compiler is perfectly capable of distinguishing type from field name in a declaration. It'd better be.
     
  7. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    Surely no one would suggest that this is good code though...

    Code (csharp):
    1.  
    2. public ScoresManager ScoresManager;
    3.  
     
    Kiwasi likes this.
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The convention across Unity documentation and unity users for public fields is camelCase. This is an article about scripting in Unity, not programming in C#. Instance fields form an important function as Unity does not natively serialise properties.

    Convention aside, naming an instance field the with the same name as the type is a bad idea, even if it does compile.