I don't understand when, in Unity, it is appropriate to use "new" For example, if I fire a rocket, I instantiate a prefab of a rocket. If I want to have a "frog" I would drop a "frog.cs" script onto a gameobject. When is it appropriate in Unity to use the new keyword? (from - http://unity3d.com/learn/tutorials/modules/beginner/scripting/data-types ) Code (CSharp): public class DatatypeScript : MonoBehaviour { void Start () { //Value type variable Vector3 pos = transform.position; pos = new Vector3(0, 2, 0); //Reference type variable Transform tran = transform; tran.position = new Vector3(0, 2, 0); } } (from - https://unity3d.com/learn/tutorials/modules/beginner/scripting/classes ) Code (CSharp): public Stuff myStuff = new Stuff(50, 5, 5); public Stuff myOtherStuff = new Stuff(50, 1.5f);
When you are creating a new object for the most part. Ideally, for beginners, anytime in C# that you encounter a situation to use Vector3, you have to use the new keyword. Setting position, rotation, and scale require a "new" vector3. If you want to create an object , you have to do so using a constructor through the "new" keyword
In Unity, for the most part you use "new" when you want to instantiate an object or create a struct that doesn't derive from MonoBehaviour.
Adding to what Fajlworks said above, the way to create a new Monobehavior-derived class is to use the .AddComponent<T>() method on a GameObject, which does the necessary behind-the-scenes plumbing to integrate the new Monobehavior with the given game object.