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. Dismiss Notice

referencing a class

Discussion in 'Scripting' started by codeBeast, Jul 22, 2021.

  1. codeBeast

    codeBeast

    Joined:
    Jun 8, 2013
    Posts:
    28
    In c sharp you create an instance of a class with the new keyword.
    In unity I have seen the referencing of an object/class done in 2 ways.

    1 - Create an instance of the class using the new keyword in the class where it is needed.

    public class Player
    public Enemy enemy = new Enemy;
    OR
    2 - Just create a variable without the new keyword then in the editor drag the class on the variable slot
    public class Player
    public Enemy enemy;
    (In the editor you will now see the enemy field - drag Enemy script to the slot.

    Is the right?
    btw: Surely the best approach is the first one. Pure OOP and you can tell immediately from the code what is going on without having to go into the editor to see what scripts have been dragged over.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    First of all, avoid using public every single time you create a variable, unless strictly necessary (and tbh, there's always never a case where public is necessary for a simple field, unless it's a dataholder class). Instead use [SerializeField] in front if you want to have unity show the field in the editor.

    Having you drag scripts or instances onto fields in the editor itself is basically the same as the first version, except there unity itself is creating that instance and then it gives you a reference to it. Makes absolutely no difference.

    Finally, the second option is better, whenever you can. Why? Well, the two options are very different. The first option should be used when your script is meant to take care of that variable. That means not for gameobjects, not for monobehaviours, not anything that should exist somewhere else. It's mainly for lists of things for example, or data, or anything of the sort.
    You can't create monobehaviours with the new keyword. It just doesn't make sense. For them to work, you would have to put them onto a gameobject. This can be done through code (AddComponent), but relying on it is simply a bad idea. You want to see what will happen in game when in the editor. Hiding the actual logic from yourself will make it much much harder to debug.
    And this last point is important, because almost nothing that can be assigned in an editor like that is creatable with new. For maintainability, the second option wins whenever it can exist.
     
    Bunny83 and codeBeast like this.
  3. codeBeast

    codeBeast

    Joined:
    Jun 8, 2013
    Posts:
    28
    Brilliant reply. Clear as water. Thank you for your time.
     
  4. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    I recall that for initialization behaviour you should use Start() and Awake in Monobehaviours. Just something that might be useful for you.
     
  5. gatewayadventuretravel

    gatewayadventuretravel

    Joined:
    Oct 7, 2021
    Posts:
    2
    is there a way i could code it so I don't have to drag it over my self
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,895
    That's a question that can only be answered with a big 'depends'. Aka, depends on what you're doing.

    Though in some cases you can design custom inspectors/editor tools to automate common tasks.
     
    Bunny83 likes this.