In order to access Class A members (variables and methods) from Class B, we need to create an instance of Class A in Class B and the syntax is:
ClassName objectName = new ClassName();
However other syntaxes seem to be doing the exact same thing.
For example, when accessing the Rigidbody component, we use the following syntax:
Rigidbody rb;
rb = GetComponent();
rb.AddForce();
Or, in this particular tutorial, in order to access the ScoreBoard script from the Enemy script, we use:
ScoreBoard scoreBoard;
scoreBoard = FindObjectOfType();
Are these all equivalent ways of doing the same thing (i.e. creating an instance of a class and accessing it from another class)?
Hi,
Actually, we do not need to create an instance. We need a reference (“link”) to an existing object/instance of Class A. ClassName objectName = new ClassName();
might create an object of ClassName but that’s not the reason why you are able to access the object of type ClassName. The reason is that an object reference got assigned to objectName
. As long as the variable references an object, you can access the object via the variable. “C#” does not care where the reference comes from.
As aforementioned, the crucial part is the reference. GetComponent<Rigidbody>()
returns a reference (or null
). If you assign the returned reference to rb
, you will be able to access the assigned Rigidbody object via rb
.
Is this what you wanted to know?
See also:
- Forum User Guides : How to mark a topic as solved
Hi Nina,
Thanks a lot for you answer. It is very helpful. So can we say that there are multiple ways to “link” to an existing object/instance (i.e. getcomponent, findobjectoftype…)?
That’s right.
Thanks a lot Nina, I really appreciate your help.
Just to make the vocabulary clear:
- When we add a Rigidbody component onto a GameObject, we create an instance/object (not a GameObject) of Rigidbody class
- Rigidbody rb creates a rigidbody type variable/reference
- rb = GetComponent() links the rb variable to the rigidbody instance/object (component)
Exactly.
Rigidbody rb creates a rigidbody type variable/reference
Not quite correct. Rigidbody rb
creates a variable of type Rigidbody, not a reference or an object.
rb = GetComponent() links the rb variable to the rigidbody instance/object (component)
Given GetComponent<Rigidbody>()
returns an object, you are right.
Thanks a lot Nina, that was very helpful. However when watching the tutorials it sounds like variable and reference are used interchangeably.
Can you please explain the difference between variable and reference?
A variable is like a box or a placeholder. If you have a smartphone and create a new contact, you have several fields to fill in: name, mobile number, etc. These are variables because their values (can) change.
Let’s say you have a friend named Rick, and his mobile number is 123456. Is that number his phone? Definitely not. It’s just a number that gets a meaning within a context. It’s a reference to his sim card if you will. If you call that number, his phone will make noises.
If your friend gets a new number, you don’t delete the contact. Instead, you simply update the “mobile phone” variable to reference his new sim card.
The same applies here in our code. Since talking precisely often takes up a lot of time, we often talk about variables when we actually mean object or object reference because we assume that the other person knows what we mean. Just like we say “my friend changed his number” instead of checking if he really just changed his number or if he actually bought a new sim card or something like that. In this case, we care more about the result (new phone number, accessing an object, etc.) than about precise phrasing.
Thanks a lot for the explanation.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.