Public Paddle paddle;

Hi,

I am very confused with gameobject to script relations within this code,

public paddle paddle;

I saw a thread asking this and below was Joao_Dalvi 's question

//
By public Paddle paddle; you are giving the name paddle to a variable of type Paddle (which is a component within the project) and saying that you are going to address it’s value by outside factors (when you say that it is public), which is the inspector in this case. Initializating this variable will allow you to cash the memory of the script that you are accessing inside it so you don’t have to look for it again later in the code that you are writing. And you have to drag the GameObject to the inspector because the engine still don’t know which instance of “Paddle” you are trying to address. You could, for example, set it just as Paddle paddle; without the public and then address the value under the void Start() method with paddle = FindObjectOfType().GetComponent();

Doing this allows you to find public variables and public methods within the Paddle Script by typing paddle.Variable/Method
//

Let say if we did

Public Paddle udemy;

instead of Public Paddle paddle; , can you somehow rephrase your answer above with udemy as variable name? It’s just that there are too many same name paddle and I am not sure which paddle you are referring to. Many thanks.

Paddle is the Type
paddle is the variable name

Notice the difference in capitalisation used.

So, for that response the re-wording would look like this;

By public Paddle udemy you are giving the name udemy to a variable of type Paddle (which is a component within the project) and saying that you are going to address it’s value by outside factors (when you say that it is public), which is the inspector in this case.

Initializating this variable will allow you to cache the memory of the script that you are accessing inside it so you don’t have to look for it again later in the code that you are writing. And you have to drag the GameObject to the inspector because the engine still don’t know which instance of “Paddle” you are trying to address.

You could, for example, set it just as Paddle udemy; without the public and then address the value under the void Start() method with udemy = FindObjectOfType().GetComponent();

Doing this allows you to find public variables and public methods within the Paddle Script by typing udemy.Variable/Method.

Please remember though, it is good practice to give your variables, methods and classes meaningful names which reflect what they are/do.

In this specific case there is only one paddle in the game so I would suggest the naming is valid. You could perhaps use playerPaddle but to have randomly chosen words or abbreviations will make things considerably more difficult in the long run, I do appreciate the request to use udemy was just for the explanation of course. :slight_smile:

Hi Rob,

Many thanks for your detailed description here. Appreciated. I am in fact, very newbie to this area, and would like to ask some further questions here(which is very basic and dumb question in a way…). from the first paragraph ,

’ By public Paddle udemy you are giving the name udemy to a variable of type Paddle (which is a component within the project) and saying that you are going to address it’s value by outside factors (when you say that it is public), which is the inspector in this case. ’

so here, where I bold and italicized, as far as I understood, ‘component’ is things that are in inspector of an gameobject something like transform or rigidbody2d. and I don’t get what ‘type Paddle’ really means. Let say, int or float, is also a variable types, so I do understand what type is in that sense, but when we designate type to certain class(Paddle), what is that mean?

I am not sure but, I thought, to recall class from other script, we initialize it, so we do something like

public Type(class name?) variable name ;

and by doing

variable name. something();

we recall functions inside that class.

But here, I am not sure how it is different from this Paddle paddle, case where we deal with gameobject.

This question here seems very unorganized and maybe doesn’t make sense at all. Let me know how it sounds to you. Thanks in advance.

Many thanks for your detailed description here. Appreciated.

You are more than welcome :slight_smile:

I am in fact, very newbie to this area, and would like to ask some further questions here(which is very basic and dumb question in a way…)

Sure thing, no problem at all. Oh, and I don’t believe any question is dumb. I think that often feel a little uneasy about asking questions as they feel that perhaps they should already know the answer and that they may be mocked for asking. Whilst it is true that in any community you may have people with more experience and may know the answer to your question, I believe there could equally be a lot of people that may not know the answer and may have wanted to ask the question themselves, but perhaps lacked the courage to do so. So, please, always feel confident about asking for help in our community, your questions could be helping a lot of other students without you even realising :slight_smile:

To answer your questions (please remember I didn’t give the initial response, so my response may be slightly different);

By public Paddle udemy you are giving the name udemy to a variable of type Paddle…

‘component’ is things that are in inspector of an gameobject something like transform or rigidbody2d. and I don’t get what ‘type Paddle’ really means.

You are correct, the Inspector displays components for the selected Game Object from the Hierarchy. As you said, a component in this sense could be the Transform, a Script etc.

Let say, int or float, is also a variable types, so I do understand what type is in that sense, but when we designate type to certain class(Paddle), what is that mean?

When you create a class you are defining a type, the following is a quote from the link below;

“A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events.”

Answering your question - it’s the same thing, Paddle is now a type, just as Int or Float are.

public Type(class name?) variable name ;

To initialise a variable of type Paddle, you could use this;

public Paddle paddle

Breaking that down;

public is the Access Modifier, this determines how the variable, in this case, can be access internally / externally

Paddle is the type, the name of the class you have defined

paddle is the name of the variable, this could be called anything you want

Note, C# is case sensitive.

variable name. something();
we recall functions inside that class.

That’s correct also.

So, if we added a method to our Paddle class, for example;

public void SpeedUp()
{
    // do stuff here
}

when you use your paddle instance you could call that method like this;

paddle.SpeedUp();

Note that I am using the lower-cased paddle as I am referring to the instance we declared, not the class (which would need a capital “P”.

This question here seems very unorganized and maybe doesn’t make sense at all. Let me know how it sounds to you. Thanks in advance.

It’s ok, I think I’ve followed along with what you were asking, I hope my answer makes sense, if not, please let me know.

With regards to GameObjects, what you are dealing with here is called inheritance, if you haven’t come across this before it may be a bit of a big subject to get into in this response but basically, you have a class that inherits from another class, this can happen a number of times.

It’s just the same as with people and their parents. I am the child of my father and my mother, I inherit certain features from them, likewise, the did from their parents and so on…

Where you have the following;

public class Paddle : MonoBehaviour
{

}

…you are stating, with the : MonoBehaviour, that Paddle will inherit from MonoBehaviour.

What this does is allows your class, Paddle, to use various members (variables, properties, methods) of the MonoBehaviour class, for example, public variables or methods, or protected methods and so on.

Now, not wanting to blow your mind too much with all of this, but MonoBehaviour also inherits from another class, typically these are referred to as a base class, and we would say that MonoBehaviours derives from it, as such it gets the use various members of it’s inherited class.

This goes back a little way, here’s a little text-based diagram (with links!) looks like this;

  • Object - Base class for all objects Unity can reference
  • Component - Base class for everything attached to GameObjects
    • Behaviour - Behaviours are Components that can be enabled or disabled
      • MonoBehaviour - MonoBehaviour is the base class from which every Unity script derives
        • Paddle - your class

As I mentioned, I didn’t write the initial response to your question, I hope that what I have provided here is of use


See also;

2 Likes

Wow… Truly appreciate your time on this. I am still digesting it but, thank you for your words here. I will thoroughly go through and might get back to you with something :slight_smile: but thanks again for your help!

Absolutely no problem, just post any questions you may have and someone will be sure to help you :slight_smile:

Privacy & Terms