Question about this variable

Hello.
public class Enemy : MonoBehaviour
{
[SerializeField] float health = 100;

 private void OnTriggerEnter2D(Collider2D other)
{
    DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
    health -= damageDealer.GetDamage();
}

}

1)Question is damageDealer variable what kind of type of variable is ?( int ,gameobject float…?)
If i move the mouse to DamageDealer it says class type what that means? bit confused

Thank you

1 Like

Hi Shaktillar,

In your code, you declared a new variable: DamageDealer damageDealer. In C#, a variable must be declared with a type, hence the type of the damageDealer variable is DamageDealer.

DamageDealer is a class you created earlier in this course. See the DamageDealer script in your Assets folder. All classes automatically become types in C#.

Did this clear it up for you?


See also:

Yes this is clear i was thinking the same.last questione then maybe it sounds stupid but…
what can hold the DamageDealer type?For example

Int numbers = 10; i know this holds whole numbers
DamageDealer ??? is it int? float? gameobject?

That’s not a dumb question at all but a very important one.

The computer does not know our human numbers, only 0s and 1s. Peope invented C# to make programming more convenient for us humans. We have multiple different integral numeric types such as byte, int and others.

In C#, we always deal with objects. int is a type. And 10, in your case, is an object of type int.

DamageDealer is a class and therefore also a type. We can create an object from that class. An object is a defined collection of data in memory, if you will. You assign that “package” to a variable of type DamageDealer.

If you find this a bit hard to grasp, maybe this example helps: Imagine your friend gave you their mobile number and you want to save it in your phone. You open the contacts and type their number into the field labelled “mobile” (or something like that).

mobile is a variable, and it expects a sequence of specific characters (here: digits). Just one digit would not work. The whole mobile number forms an object. And this object gets assigned to mobile. Then your smartphone will be able to process that data. When you click on the phone number, you can call your friend. Another example would be a birthday or any other date.

In C#, you have a data type named DateTime. Even though the date consists of integers, it’s a predefined collection of data. And if you create a “date”, you create an object of type DataTime.

Super Clear .Thank you .

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

Privacy & Terms