Search Unity

Why doesn't my script work? ( GameObject.FindGameObjectsWithTag )

Discussion in 'Scripting' started by Larpushka, Jul 1, 2015.

  1. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    So this script I wrote is supposed to make all the objects in the screen that are tagged "clickable" glow. It doesn't work, nothing happens.

    For the record, I put this script on the main camera... it then gave me an error that the camera doesn't have an halo component. I don't understand why it needs an halo component if it's not tagged "clickable", it's tagged as "main camera", so I added an halo component to the camera.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FindObjects : MonoBehaviour
    5. {
    6.     public GameObject[] sceneobjects;
    7.    
    8.     void Update ()
    9.     {
    10.         if (Input.GetKeyDown ("space")) {
    11.             sceneobjects = GameObject.FindGameObjectsWithTag ("clickable");
    12.             for (int i = 0; i < sceneobjects.Length; i++) {
    13.                 Behaviour h = (Behaviour)GetComponent ("Halo");
    14.                 h.enabled = true;
    15.             }
    16.         }
    17.         else
    18.         {
    19.             Behaviour h = (Behaviour)GetComponent("Halo");
    20.             h.enabled = false;
    21.         }
    22.  
    23.  
    24.     }
    25. }    
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Without an object reference, GetComponent used by itself will get component on the object that the script is running on
    you need to index the array that you found like this:
    Code (CSharp):
    1.  
    2.             for (int i = 0; i < sceneobjects.Length; i++) {
    3.                 Behaviour h = (Behaviour) sceneobjects[i].GetComponent ("Halo");
    4.                 h.enabled = true;
    5.             }
    and ofc you have to run the whole loop again to turn them all off
    it's also worth using the generic getComponent
    http://docs.unity3d.com/Manual/GenericFunctions.html
     
  3. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
  4. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    Thank you! However, another problem emerged. Right now when I press "tab" it only seems to work for a frame. How can I make it so it'll work as long as "tab" is pressed.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FindObjects : MonoBehaviour
    5. {
    6.     public GameObject[] sceneobjects;
    7.  
    8.     void Update ()
    9.     {
    10.         if (Input.GetKeyDown ("tab")) {
    11.             sceneobjects = GameObject.FindGameObjectsWithTag ("clickable");
    12.             for (int i = 0; i < sceneobjects.Length; i++) {
    13.                 Behaviour h = (Behaviour) sceneobjects[i].GetComponent ("Halo");
    14.                 h.enabled = true;
    15.             }
    16.         }
    17.         else
    18.         {
    19.             sceneobjects = GameObject.FindGameObjectsWithTag ("clickable");
    20.             for (int i = 0; i < sceneobjects.Length; i++) {
    21.                 Behaviour h = (Behaviour) sceneobjects[i].GetComponent ("Halo");
    22.                 h.enabled = false;
    23.         }
    24.  
    25.  
    26.     }
    27. }
    28. }
    If it's on "Start" and not in "Update" it doesn't work at all
     
  5. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    OK I found the answer...should've used "GetKey" instead of "GetKeyDown"...thank you!!!
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Well kind of
    If you have GetKey, it will execute the code every frame
    you could move each half into a seperate block, one for key down to turn them on, one for key up to turn them off
     
  7. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    Yea, but it's more comfortable to use a single key IMO, though it's more "system consumptive" as you indicate. I just like it the way it was done in the game Neverwinter Nights, so I wanted to imitate that :)
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The user would never know the difference. You should definitely toggle with GetKeyDown and GetKeyUp
     
  9. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    Why "definitely"? What if I want to be a certain way despite it being more overload on the system? Is it such a game-design sin?
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    What way do you "want it"? My point was that using GetKeyDown/Up is not only better for performance but the end result from the user's perspective is identical.
     
  11. arty155

    arty155

    Joined:
    Apr 17, 2015
    Posts:
    22
    What I believe hpjohn meant was to do the following:
    Code (csharp):
    1. if (Input.GetKeyDown ("tab"))
    2. {
    3. // Turn on the Halo
    4. }
    5.  
    6. if (Input.GetKeyUp ("tab"))
    7. {
    8. // Turn off the Halo
    9. }
    It's the same key for both actions. KelsoMRK was stating that the player would never know that there are 2 separate actions going on behind the scenes; one to begin the halo, one to stop it.
     
    KelsoMRK likes this.
  12. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    But what if I want the player to "Hold" the tab key if he wants this action to continue? I don't want him to click once to enable it and click another time to disable it. In this case, I'm using correctly GetKey and not GetKeyDown, right?

    By the way, from some reason I can't use this script along with this script:

    It seems the script with the "tab" disables this script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Highlight : MonoBehaviour {
    5.  
    6.     void OnMouseEnter()
    7.      
    8.     {
    9.         Behaviour h = (Behaviour)GetComponent("Halo");
    10.         h.enabled = true;
    11.     }
    12.     void OnMouseExit()
    13.     {
    14.         Behaviour h = (Behaviour)GetComponent("Halo");
    15.         h.enabled = false;
    16.     }
    17. }
    18.  
     
    Last edited: Jul 5, 2015
  13. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You seem to be overlooking this really obvious thing
    What happens when you hold a key?
    You have to press the 'KeyDown'
    And then you hold it (during which you don;t really need to execute anything new)
    Then you let the 'KeyUp' to stop holding


    OnMouseEnter only works on colliders
     
  14. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    I have colliders, the script I wrote in my last post works. However, when I add the script with the "GetKey" is suddenly doesn't work. Nevertheless, I got them both to work together, indeed all I needed to do was to add "GetKeyUp" to the else if statement :) Apparently not including that caused this entire problem. You were right! Thank you.