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

learn Script

Discussion in 'Scripting' started by F18, Apr 12, 2015.

  1. F18

    F18

    Joined:
    Mar 23, 2015
    Posts:
    18
    when I using script, using UnityEngine, using System.Collections and other. I ever see script without that command, what different and function?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Huh?

    Are you talking about the 'using' command at top of the code file?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SomeCustomScript : MonoBehaviour
    6. {
    7.      ///... do stuff
    8. }
    9.  
    Those 'using' commands define which namespaces are accessed locally.

    Groups of classes, structs, enums, etc, can be put into a namespace. This namespace acts like a surname.

    Give you an example:

    UnityEngine.Vector2
    http://docs.unity3d.com/ScriptReference/Vector2.html

    Microsoft.Xna.Framework.Vector2
    https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.vector2.aspx

    They are distinctly different structs, yet they're both called 'Vector2'. With out the namespace, and if you had both libraries imported, the compiler wouldn't know which Vector2 to use anytime you said Vector2. So instead you would say its 'fully qualified name' (that being the long handed UnityEngine.Vector2).

    BUT, it'd be annoying to say that EVERY time. So in the header of the file you can just say "using", which just says that if you type some name of a class/struct/etc, and it doesn't have the fully qualified name... checks the 'used' namespaces for a class/struct/etc that matches the name.
     
  3. christinanorwood

    christinanorwood

    Joined:
    Aug 9, 2013
    Posts:
    402
    System.Collections is not used in many basic scripts and could be omitted unless you're using List or something like that in your code. You need UnityEngine for just about everything though.
     
  4. F18

    F18

    Joined:
    Mar 23, 2015
    Posts:
    18

    namespace Assets.Code.Interfaces
    {
    public interface IStateBase
    {
    void StateUpdate():
    void ShowIt():
    }
    }
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    o_O?
     
    Kiwasi likes this.
  6. christinanorwood

    christinanorwood

    Joined:
    Aug 9, 2013
    Posts:
    402
    I just mentioned the book that code comes from in another thread!! I really like his approach, but you only need that namespace if you're writing non MonoBehaviour classes.
     
  7. F18

    F18

    Joined:
    Mar 23, 2015
    Posts:
    18
    approach? what is mean it? I discover that script in fdp Learning C# by Developing Games with Unity 3D, so I ask this forum why that script not use "using UnityEngine, Assets.Collections.....
     
  8. christinanorwood

    christinanorwood

    Joined:
    Aug 9, 2013
    Posts:
    402
    In Terry Norton's book the IStateBase interface does not use any Unity classes (such as GameObject, Transform, etc) and so does not need to have the statement using UnityEngine, which is the namespace that contains all those classes. However the specific states such as BeginState, which implements the IStateBase interface, does use some Unity classes such as Application (a Unity class) and the GUI class, so that file does need to have using UnityEngine at the start.

    Because he puts code in subdirectories of the Assets directory, he needs to specify that as the namespace for his classes (the ones that do not inherit from MonoBehaviour), and any class that uses them needs to have a using statement at the start or the program won't be able to find them.
     
  9. F18

    F18

    Joined:
    Mar 23, 2015
    Posts:
    18
    is example that script(IStateBase) always use every make command/script, or is it dependent maker script.
     
  10. christinanorwood

    christinanorwood

    Joined:
    Aug 9, 2013
    Posts:
    402
    In any script the using statements that you should include depend on what classes are used in the script. Any standard Unity script is a child of MonoBehaviour and this is part of UnityEngine namespace, so you should have a using UnityEngine; at the start. If your script uses the new UI classes then you should also have using UnityEngine.UI; at the start.

    I hope I have understood your question correctly.
     
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    MonoBehaviour is inside of UnityEngine. IEnumerator is inside of System.Collections. As these are both heavily used inside of most Unity scripts they are included in the default script template.

    If you are not using the namespaces simply delete them.
     
  12. F18

    F18

    Joined:
    Mar 23, 2015
    Posts:
    18
    oh thank you was explained and I am sorry if my ask overmuch.