Variables and Types: Bank bank

For this lesson we made modifications to the Enemy.cs script so that, on death, they would deposit coins into our bank.

I’m very new to coding and the actual roles that “Bank bank” and " bank = FindObjectOfType();" play are a little unclear to me.

Am I correct in thinking that “Bank” is a type of variable and “bank” is the name of the variable? Why is “Bank” the type of variable we are using? Is it because we are referencing the Bank.cs script we wrote previously or is there another reason for the name, or can we name these whatever makes sense for us? For instance, could we have named our type and variable PiggyBank piggyBank?

Secondly, what is " bank = FindObjectOfType(); " doing for the computer? We’ve already told it what type of variable “bank” is, is it locating the Bank.cs script or something else?

Any help is greatly appreciated!

Here’s the bits of relevant code from our Enemy.cs script:

public class Enemy : MonoBehaviour

{
 
  Bank bank;

  void Start()
  {
          bank = FindObjectOfType<Bank>();
  }
  
   public void RewardGold()
   {
      if(bank == null) { return; }
      bank.Deposit(goldReward);
   }

}

Hi @Veda_OuO,

Welcome to our community! :slight_smile:

I read that you were very new to coding. Before I answer any of your questions, I’d like to know if you completed the previous sections. The “Realm Rush” project is fairly complex and is based on concepts that were covered in the previous sections. If you feel that you are struggling understanding the C# syntax, I would strongly recommend to start with the “Obstacle Course” section instead of starting with the most difficult section in this course.

Here is a brief summary/ answer for you:

Bank is a class and a type. bank is the variable name. FindObjectOfType<Bank>() returns a reference to a Bank object or null. Names in C# do not have any inherent meaning. You may name your classes, variables and methods as you wish as long as the names are unambigous within the current scope.

Did this clear it up for you?


See also:

1 Like

Thanks for the response, Nina. Yes, I completed Obstacle Course and Project Boost and found them to be extremely helpful and well-designed courses.

Your explanation helps, and I’ve also done some additional reading, but I’m still a bit unclear on the precise implications of declaring: Bank bank;

Say the rest of our script is empty, what have we accomplished by typing Bank bank; ?

FindObjectOfType seems to imply we’ve created or at least referenced an object. Have we created a completely new object?

I think part of my confusion stems from the fact that we have three separate entities named by a single term: Bank. We have a script called Bank, we have a game object in our hierarchy called Bank, and now we have a class called Bank.

I completely understand the function of the script and game object called Bank. Is our new class Bank completely separate from these other Bank entities supposing we’ve only typed Bank bank; ?

Sorry if I’ve belabored the point, I’m just trying to ask the question as clearly as I can haha. Thanks again for any clarity you can offer.

Given there is a class named Bank, we could declare a variable of type Bank in the empty script/class. Of course, variables do not do anything, so just having Bank bank; in an otherwise empty class is a waste of resources.

The “Bank” game object in the Hierarchy does not have anything to do with it. Forget it for a moment. Just focus on the C# side:

  • We have a script named Bank.cs.
  • Inside the Bank.cs script, we have a class named Bank.
  • Attached to the Inspector of a game object in the Unity Inspector, we have a Bank component.

The component is the relevant part. The component represents a C# object.

Classes are basically blueprints for objects. If you need an analogy: A class is like the blueprint of a car, and the object is the actual car that you can drive.

Back to our variable bank: By default, variables of reference types are null (= no object reference). There is no object attached to it. We need an object to be able to do something because the Bank class is just a blueprint.

Reference types are all types that are not value types. Bank is a class, thus it is a reference type.

If you find this whole “Bank” stuff confusing, feel free to rename your variable. Bank myBank; is perfectly fine. It’s just common practice to name your variable after the class if there is nothing special about the object. We just want “one bank object”, and since we are programmers, not poets, we simply name the variable bank. It’s the only variable of type Bank in our class anyway.

And what does the game object named “Bank” have to do with this? Nothing. We just named it “Bank” in the Unity Editor because we don’t want to guess around what the purpose of this game object might be when we open Unity the next time. We are also not interested in the details anymore once our “bank” works as expected.

I hope this made sense. :slight_smile:

3 Likes

I think I’ve got it straight now. Thank you so much for the thorough explanation! A massive help!

1 Like

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

Privacy & Terms