Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Understanding GameObjects

Discussion in 'Getting Started' started by geranimo, Dec 24, 2015.

  1. geranimo

    geranimo

    Joined:
    Dec 18, 2015
    Posts:
    6
    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

    Code (CSharp):
    1. [HideInInspector] public GameObject m_Instance;
    and after we use this instance to do

    Code (CSharp):
    1. m_Shooting = m_Instance.GetComponent<TankShooting>();
    2. 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.
     
    Last edited: Dec 25, 2015
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    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.
     
    jhocking and geranimo like this.
  3. geranimo

    geranimo

    Joined:
    Dec 18, 2015
    Posts:
    6
    Thanks !