When to use "= new ScriptName"

I’ve been a bit confused recently on some syntax of referencing other scripts. I’ve got the concept of GetComponent() down, and I generally just create a variable where the type is the script name so I can hold the reference to that script. Something like this, for example:

OtherScript myOtherScript;

void Start()
{
  myOtherScript = someObject.GetComponent<OtherScript>();
}

Makes sense and works just fine. But where I get confused is in looking at some example code, like Unity’s own tutorials, where they sometimes create a script reference variable and set it equal to “new” scriptname. For example:

OtherScript myOtherScript = new OtherScript();

I thought I understood this previously. I thought this was used to reference scripts that don’t inherit from monobehaviour so that the “new” keyword is essentially instantiating that script into your current script so that you can run functions and such from it as if the code existed in your current script, since the other script doesn’t inherit from monobehaviour and thus couldn’t run functions that require it. However, the more I look into this it seems that this is not entirely correct. When I look at Unity’s “Property” scripting tutorial…

http://unity3d.com/learn/tutorials/modules/intermediate/scripting/properties

…they use this syntax with the “new” keyword to reference another script (which doesn’t inherit from monobehaviour) even though the only thing they’re referencing from that script is variables, which I thought didn’t require the “new” keyword. So now I’m just really confused.

What is the difference between the below 2 lines of code:

OtherScript myOtherScript;

OtherScript myOtherScript = new OtherScript();

No you are entirely correct in your initial understanding. If a script doesn’t derive from MonoBehaviour, it can’t be added to a game object through the editor or through code by using the AddComponent() method.

When you say “OtherScript myOtherScript;”, you are just setting aside a pointer to an existing script, which if left like this is pointing to null, so no script. When you say “new OtherScript()”, you are constructing a new script and in the code above assigning it to this pointer (reference technically speaking). If that script contains variables that are not static, then you must create an instance of the script (new above) to access those variables. Static variables are created at program startup, so in those cases you can say “OtherScript.whatever” if whatever is declared static. The point about static variables is there is only one variable for all OtherScript script instances, so setting it in one script changes it everywhere.

Don’t confuse “reference” with “new”, entire different things. A reference simply points to an instance somewhere in memory. New creates said new instances. For instance (no pun intended), I could say:

OtherScript myOtherScript;
OtherScript myOtherScriptRefNumber2;

myOtherScript = new OtherScript();
myOtherScriptRefNumber2 = myOtherScript;
myOtherScript = new OtherScript();
new OtherScript();

This will create 3 instances. The first one is created, then RefNumber2 points to it, and then we use myOtherScript to create a second instance. Lastly, the last line creates a new object in memory but that place is not stored, so no one can ever access that object again (unless something inside the object tells something else where it is, but that’s beyond this example).

Lastly, variables and methods are the same. If they are not static, then an instance is required to access them. You can have static variables and methods as mentioned above as well, in which case there will only be 1 copy of the variable for all instances and similarly a static method can only access static variables and thus will operate on variables used by all instances.

A class is a template for creating things from and doesn’t do anything on its own (except for ‘static’ elements). You can’t access non-static elements of a class without an instance of the class.

When you create a ‘new’ ClassName (not scriptname; only Unity’s Objects (MonoB, scriptableObject, etc) require the class and file names to match), you are creating one instance of that template. Every instance has its own copy of all the (non-static) variables.

Your two lines of code: The first, “OtherScript myOtherScript” is creating a variable which can hold an OtherScript instance but which is initialized to “not pointing to any instance”. The second line creates the same variable but initializes it to a new instance.

Generally the first line is shorthand in Unity when dealing with Unity Objects for “this is a thing that should be drag’n’dropped in the inspector”.