Search Unity

Proper way to tell my script about a gameobject for manipulation?

Discussion in 'Scripting' started by vit_the, Feb 16, 2018.

  1. vit_the

    vit_the

    Joined:
    Feb 3, 2018
    Posts:
    13
    Hello,

    I have a script working at the open of a level called OnGameStart. Its fine.

    However I want to give this script control over the appearance of certain GameObjects.

    For example, I want it to turn off some gameobjects so the user doesn't see them until some unrelated tweens play and stuff happens.

    I am having trouble causing the script to recognize what object it needs to manipulate and what it can do with that object.

    Here's what I have related to my desired functionality:


    1. An object in my Hierarchy called HeroPowerButton -- two of them are in the scene. I want these to appear in the scene only after certain starting sequences.

    Code (CSharp):
    1.  
    2. ...........
    3.     public HeroPowerButton playerHeroPowerButtons;
    4. ...........
    5.  
    6. In OnGameStart()
    7.  
    8.        playerHeroPowerButtons.SetActive(false);
    9.         Invoke("TurnOnHeroPowerButtons", 3f);
    10. ...............
    11.  
    12.  
    13.     private void TurnOnHeroPowerButtons()
    14.     {
    15.            playerHeroPowerButtons.SetActive(true);
    16.  
    17.     }
    18.  
    I get the same error if I include in Awake()
    Code (CSharp):
    1.  
    2.         playerHeroPowerButtons = GetComponent<HeroPowerButton>();
    3.  
    I can't get it to compile because "HeroPowerButton does not contain a definition for SetActive". I thought every gameobject understood SetActive.

    Am I not supposed to give my GameObject HeroPowerButton the name playerHeroPowerButtons and then use the new name I gave it for the GameObject's manipulation?
    https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html


    I appreciate you reading & help on wrapping my head around how scripts can work with gameobjects.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    You need to do
    playerHeroPowerButtons.gameObject.SetActive(false);

    You have to target the gameObject if you link it by script, otherwise, there is no way of knowing this is a GameObject. As you declared it, it's a reference to an instance of the script.
     
  3. vit_the

    vit_the

    Joined:
    Feb 3, 2018
    Posts:
    13
    Alright! Thank you. It works!

    For anyone having trouble Linking Objects From Hierarchy To Public Fields In Unity's Inspector here's one solution:

    When the script compiled, I noticed I could not drag and link the gameobject(s) in the scene called HeroPowerButton(s) from the Hierarchy to the inspector field "player Hero Power Buttons" variable I declared in my script with the HeroButtonPower:

    Code (CSharp):
    1. "public HeroButtonPower playerHeroPowerButtons"
    .

    I changed the declaration to
    Code (CSharp):
    1. public GameObject playerHeroPowerButton1
    2. public GameObject playerHeroPowerButton2
    and it works like a charm altogether.

    by the way what is that 2nd field in the variable called? Is it Type GameObject I declared properly instead of Type HeroButtonPower?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Well, variables have a type. So GameObject is the type and you can tie any gameobject to that variable. If you declare as a HeroButtonPower type, then you have to have a gameobject with that script on it and then you can attach that gameObject to that variable as it has an instance to that script on it.

    Which type you use depends on what you plan to do with it. If you use the script, then you don't have to use GetComponent to access the script on the gameObject as you already have a reference to it. I generally prefer this as I can then just use the .gameObject to get to the gameObject the script is attached to.

    variables are declared (for the basic variable)
    accessor type variableName
     
    vit_the likes this.