Search Unity

Do I always need to have these default lines of code?

Discussion in 'Scripting' started by surajsirohi1008, Oct 22, 2017.

  1. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    267
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;


    These lines of code are created by default when we create new script, but do we always need it? All 3 of it?
     
  2. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    No, they are just some commonly used namespaces, use them when you need them, they aren't required.

    For example, the IEnumerator that is used in Unity Coroutines is available at System.Collections namespace, so it is needed.

    And also, the List<T> and IEnumerator<T>, ... all are available at System.Collections.Generic.

    And most importantly, the Unity components and classes are available at UnityEngine namespace, for example, the MonoBehaviour class is available at UnityEngine.

    They aren't required, they are just most commonly used.

    Thanks.
     
    surajsirohi1008 likes this.
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    sometimes yes sometimes no.

    They are including namespaces for you to use in your Scope.
    UnityEngine is required for pretty much all basic Unity api stuff, including MonoBahviours
    System.Collections is included by default because coroutines require the IEnumerator type that is in it.
    System.Collections.Generic is used by almost all generic collection types, List<T>, Stack<T>, Dictionary<K, V> etc.
     
    surajsirohi1008 likes this.
  4. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    267
    Alright, is there a page where I can see which namespace is required for which components?
     
  5. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    surajsirohi1008 likes this.
  6. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    267
    hasanbayat likes this.
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,147
    You never truly need any of them. You can write complete programs without ever having them in your code. That said if you remove them you'll need to refer to the namespace every single time you wish to use something contained in it.

    With using:
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class Foo : MonoBehaviour
    4. {
    5.     public void Start()
    6.     {
    7.         Debug.Log("Hello world!");
    8.     }
    9. }
    Without using:
    Code (csharp):
    1. public class Foo : UnityEngine.MonoBehaviour
    2. {
    3.     public void Start()
    4.     {
    5.         UnityEngine.Debug.Log("Hello world!");
    6.     }
    7. }
    You can safely remove the ones you're not using but there are no real advantages to doing so other than to keep your code clean, reduce the likelyhood that you run into a name clash (two namespaces using the same name for part of their content), and to reduce the contents of the auto completion dialog.

    Worst case if you remove one that you needed you'll just get errors from the compiler about how it can't find the class, enum, etc that you were referring to and asking if you forgot to include the namespace.
     
  8. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    267
    No advantages? No improvement in performance or memory?
     
  9. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    630
    There are no advantages, they are just the look of the code.

    If you want clean code, add use statements at top of script.
     
    surajsirohi1008 likes this.
  10. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    267
    also could you tell me how to embed those lines of like you did, in here, in the forums, I'm new.
     
  11. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
  12. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    267
  13. surajsirohi1008

    surajsirohi1008

    Joined:
    Jun 21, 2016
    Posts:
    267
    alright, thank you for your time.
     
    hasanbayat likes this.