Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Some basic scripting help

Discussion in 'Scripting' started by amon_unity952, Oct 2, 2018.

  1. amon_unity952

    amon_unity952

    Joined:
    Oct 2, 2018
    Posts:
    3
    Hello I could use some basic help. I've watched a bunch of tutorials including the ones made by Unity but I can't seem to find one in depth enough to explain some concepts. I've searched high and low so I'm hoping someone can help. Although there are a lot of great tutorials most skip over the details that I need or they just go to fast.

    So I need to understand two things.

    1) When do I have to use the keyword "new" and when is it not needed? It seems random. Sometimes x =x+y is ok and sometimes it has to be x = new x + y. I know this isn't a perfectly coded example but just an idea of my lack of understanding.

    2) Sometimes it seems you can refer to gameObject and it will reference the game object attached to the script and sometimes you have to type this.gameObject. If the game object is attached to the script why would you sometimes have to write this.gameObject?

    Thanks for the help.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    1. Generally new is used when you are making a new object such as a class or struct and it allocates the actual piece of memory that will contain that object. Simple data types like int and long don't need it, but you actually CAN do it:

    Code (csharp):
    1. int i = new System.Int32();
    There's probably arcane language needs for that to be complete, if not just to have an orthogonal language syntax. Someone who cares more about language details can add to this!

    2. Using "this." to access all member variables (.gameObject is a member variable of MonoBehaviors through inheritance) is the pedantic way to be clear what you are doing. As long as there is no other local variable or argument by the same name, you technically don't need it. Some people swear by it, some people think using "this." everywhere just clutters up your code.

    Let me take a pedantic sidetrip though: GameObjects are not "attached" to scripts. Every object in the scene graph is a GameObject. Every GameObject can have Components plugged into it. ALL GameObjects have either a Transform or a RectTransform Component plugged in automatically.

    Other Components include Monobehaviors and all the other "blocks" that you see showing up in the inspector. This is a component architecture: components are plugged int the base GameObjects. Consequently, you cannot just new() up a MonoBehavior. You have to use .AddComponent to stick it onto an existing GameObject.

    This mechanism of sticking it on via AddComponent triggers all the necessary connections and bookkeeping within the engine, and this is what gives MonoBehaviors their "first class" access to the GameObject itself (via the .gameObject property) and the Transform component (via the .transform property).
     
    Ryiah likes this.
  3. amon_unity952

    amon_unity952

    Joined:
    Oct 2, 2018
    Posts:
    3
    Appreciate the quick response. Unfortunately for someone fairly new to this, the explanation isn't easily understood. I kind of need this explained in a more logical less technical way if anyone can do that. Thanks.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    The super-simple version:

    new - you're making a new thing that's not a primitive, which is all of the numeric types (int, float, etc.) and bools. Everything else than those you create through the new keyword.

    this. - pretty much only necessary if you have two variables with the same name:

    Code (csharp):
    1. public class Script : MonoBehaviour {
    2.  
    3.     public int foo;
    4.  
    5.     public void SomeMethod(int foo) {
    6.         int a = foo; //this "foo" is the foo parameter to SomeMethod (one line up)
    7.         int b = this.foo; //this "foo" is the foo that's a field of Script (the "public int foo" up there.
    8.     }
    I'd recommend you try to understand @Kurt-Dekker's reply, though, it'll help a bunch. Read it a couple of times, google the terms you don't get, etc.
     
    Kurt-Dekker and Ryiah like this.
  5. amon_unity952

    amon_unity952

    Joined:
    Oct 2, 2018
    Posts:
    3
    Thanks. appreciate the simple version. now I will go back and try to understand the complex version a bite at a time.
     
    Kurt-Dekker likes this.