Understanding GameObjects

Hello, I am following the tank tutorial and I don’t understand the TankManager code. (details are not important)

I have a class named TankManager in which we define

[HideInInspector] public GameObject m_Instance;

and after we use this instance to do

m_Shooting = m_Instance.GetComponent<TankShooting>();
m_CanvasGameObject = m_Instance.GetComponentInChildren<Canvas>().gameObject;

I don’t understand the last 2 lines of code. How does this work? m_Instance has no value so how GetComponent is able to find TankShooting? TankShooting is not in my hierarchy, nor is Canvas. It’s like the script knows that we want m_Instance to be an instance of a tank, but we never did that in the code.

You’re right, that’s not going to work. Because of the [HideInInspector] tag, you can’t assign m_Instance to some GameObject in your scene using the inspector, which is the usual way these things are hooked up.

So, there would have to be code somewhere that assigns it. Because it’s public, but hidden in the inspector, the intent must be to assign to this from some other script (on the tank maybe).

This is generally not a good way to do things, but who knows, the tutorial author probably had a good reason for it.

2 Likes

Thanks !