I am a novice game maker taking the Complete Unity 5 Developer course.
I am using C#.
I am a little confused as to when to use:
- Instance of something. If I am in a script and referring to the object the script is attached to, if I want to refer to the object the script is attached to, do I use Instance?
- this.
- gameObject.
- Object Name. (ex. paddle.)
- new (ex. new Vector3 (…)
Thanks,
Arthur Sterling (magicscreen)
- Yes, if you want to refer to the object the script is attached to you’ll use an instance. A class (your scripts in Unity) is much like a blueprint that lays out how the object is - the properties it has, functions it has, etc. You create objects according to the class. That is, you create instances of the object.
What I mean is you could have a class called Player. You could have multiple players, each having an instance of this Player class. If you want to refer to an individual one of those players, you would refer to that particular instance of it. When you use the word “static”, you’re talking on a class level. In this case, all instances of Player will share it as it applies to all of them.
- ‘this’ refers to the class instance you use it within. Generally, the only time I find it to be useful is a case such as this:
public class SomeClass
{
int x;
int y;
public SomeClass(int x, int y)
{
this.x = x;
this.y = y'
}
}
I pass in variables named x and y in the SomeClass constructor, and then set them to x and y. How does the compiler know that when I put the first x and y, I’m talking about the class members at the top instead of the parameters coming into the function? That’s what “this” is for. It says that I’m talking about the class instance variables.
-
gameObject refers to the GameObject your script is attached to. You shouldn’t have to use it very often, but there are cases where it’s handy. One of those may be that you receive a collider from a OnColliderEnter message and want to get at the GameObject that the collider is attached to. You would use: collider.gameObject to do so. Then you can get at that GameObject’s other components or receive other information from it.
-
I’m not entirely sure what you mean here. Do you mean the name of your GameObject in the scene? You can find objects by name using GameObject.Find, but I wouldn’t recommend it. There are always better alternatives available for finding objects in a much faster way. You could find other uses for using the name, though. Maybe you have a UI system with several buttons, and you send the GameObject the button is attached to (or its name) to the button handler function. You can use the name to figure out which button was clicked with the same function.
That’s one example, I’m sure you can find more ways to use it if you care to.
- “new” is used whenever you’re creating a ‘new’ instance of a struct or a class. Vector3 is a struct, so to make a new one you will use the new keyword. And to make a new player (in typical C#), you would use ‘new Player()’. In typical C# programming, you always use the new keyword to make instances of your “scripts” (classes). In Unity, however, classes can inherit from MonoBehaviour - this is the default when making a new script and it’s required to attach your scripts to objects.
When inheriting from MonoBehaviour, you typically don’t use ‘new’ like you normally would. The engine creates your instance for you when you drag it onto the object. But you can think of dragging it onto the object as similar to using the ‘new’ keyword in code.
1 Like
JasonBricco,Thanks for your feedback.
Great explanations.
Art Sterling