Confused about variable type

Hello all,

Back in lecture 12, I gathered that there are 3 parts to a variable: the Name, Type and Value and if we wanted to create a variable called “Speed” and initialized it with a value of “10.5”, then we can write the variable as:

float speed = 10.5;

I understand what the type “float” is doing here. Similarly, I understand what a type like bool or integer or string is doing when we define a variable that goes along with it. However, cut to this lecture and now I am looking at:

MeshRenderer meshrenderer;
Rigidbody rigidbody;

and I don’t really understand what the “type” is doing here. I know its saying the variable meshrenderer has to be of type MeshRenderer, but what does that exactly mean in this context? What does it mean to have a component of the UnityEngine class as a type for a variable? Why doesn’t it work if we write it as:

string Meshrenderer;
string rigidbody;

I am just a little bit unclear about this issue so if someone can help me out that would be amazing. Cheers!

Everything has a type. The mesh renderer is of type MeshRenderer. You can have string meshRenderer if you want, but then you have a string variable called ‘meshRenderer’ and not a MeshRenderer. It doesn’t work as a MeshRenderer because string is a different type, and you can’t put the value of one type into another (well, you can but it needs to be defined). Just like we can’t say

float speed = true;

A type defines everything about the variable we defined. If the type is float, we can do floating point arithmetic with it, it will hold a decimal value and comply to floating point rules. Similar to bool. If it is a bool we know that it will have 2 states, either ‘true’ or ‘false’ because that is what a bool is defined to do. A MeshRenderer is another ‘type’ of variable. It can do all the things that was defined for the MeshRenderer. Even when we create our own components, they are types. If we make a Movement component, that is a type that defines ‘movement’ and we can put that in a variable and use it. If we defined a public MoveToTarget() method on it, we can call it somewhere else by creating a variable of our Movement type and calling the method on that type

Movement movement;
// get an instance of the movement somewhere
movement = FindObjectOfType<Movement>();
movement.MoveToTarget();

I’m not a teacher, so it’s hard for me to explain, sorry.

1 Like

I think I am getting it, it might be something that I can fully visualize with practice but I think, basically, we are just associating a component with the variable when we type something like Rigidbody rigidbody;

Well, no. Just typing MeshRenderer meshRender does nothing but define a variable of type MeshRenderer. The value of that variable will not be set. In order to set it, we’d need to either

create a new instance of that using conventional C# (which Unity doesn’t want us to do with Unity objects)

MeshRenderer meshRenderer = new MeshRenderer();
// or
MeshRenderer meshRenderer;
meshRenderer = new MeshRenderer();

or the Unity way is to instantiate the objects

MeshRenderer meshRenderer = Instantiate<MeshRenderer>(_somePrefabWithAMeshRenderer);
// or
MeshRenderer meshRenderer;
meshRenderer = Instantiate<MeshRenderer>(_somePrefabWithAMeshRenderer);

or add it to a game object

MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
// or
MeshRenderer meshRenderer;
meshRenderer = gameObject.AddComponent<MeshRenderer>();

or get it from a game object that already has one

MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
// or
MeshRenderer meshRenderer;
meshRenderer = gameObject.GetComponent<MeshRenderer>();

Only once we did this will we have an association (reference) between a specific component and the variable

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms