Search Unity

FindGameObjectWithTag suddenly stopped working

Discussion in '2D' started by MoonJellyGames, Oct 21, 2014.

  1. MoonJellyGames

    MoonJellyGames

    Joined:
    Oct 2, 2014
    Posts:
    331
    This is why versioning is important! Everything that I had implemented in my 2D puzzle/platformer so far was working beautifully. Then I tried to add a new animation, and it all fell apart.

    I made these handy diagrams to get my game concept across quickly:


    So I have a game object called CycleManager that handles the switching between players in the scene. Currently, you can only cycle in one direction, though that's something I'll be changing. Here is the CycleManager code, which is where the problem seems to be happening:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerCycleController : MonoBehaviour
    5. {
    6.  
    7.     GameObject[] players;
    8.  
    9.     void Start ()
    10.     {
    11.  
    12.     }
    13.  
    14.     void Update ()
    15.     {
    16.         if (Input.GetKeyDown(KeyCode.C))    //DS3 Triangle button
    17.         {
    18.             cycleSelected();
    19.         }
    20.     }
    21.  
    22.     void cycleSelected()
    23.     {
    24.         players = GameObject.FindGameObjectsWithTag("Player");    //the array contains all of the players in the scene
    25.         int tempID = 0;
    26.      
    27.         for (int i = 0; i < players.Length; i++)
    28.         {
    29.             if (players[i].gameObject.GetComponent<PlayerController>().selected)
    30.             {
    31.                 players[i].gameObject.GetComponent<PlayerController>().deselect();    //deselect the current player
    32.                 tempID = players[i].gameObject.GetComponent<PlayerController>().playerID;    //store the ID of the recently-selected player
    33.             }
    34.         }
    35.  
    36.         for (int i = 0; i < players.Length; i++)
    37.         {
    38.             if (tempID == players.Length)    //if the recently-selected player was the last player in the order
    39.             {
    40.                 players[0].gameObject.GetComponent<PlayerController>().select();    //select the first player
    41.                 break;    //and exit the loop
    42.             }
    43.             else if (players[i].gameObject.GetComponent<PlayerController>().playerID == (tempID + 1))    //if the player at index i after the selected player
    44.             {
    45.                 players[i].gameObject.GetComponent<PlayerController>().select();    //select the player
    46.                 break;    //and exit the loop
    47.             }
    48.         }
    49.     }//endof cycleSelected
    50. }//endof PlayerCycleController
    51.  

    When I press Triangle, I get this error:

    NullReferenceException: Object reference not set to an instance of an object
    PlayerCycleController.cycleSelected () (at Assets/Scripts/PlayerCycleController.cs:29)
    PlayerCycleController.Update () (at Assets/Scripts/PlayerCycleController.cs:18)



    It's referring to the line where the elements in the array are checked to see if their "selected" value is set to true. It seems like the players array isn't being loaded for some reason, though I have no idea why not because it was working before.

    The changes I made were minor: I attempted to add the animaton that plays when a player transitions from not selected to selected. I set up the animator with a transition that would go from any state to "playerSleep" (the name of the animation) with a trigger. I haven't spent a lot of time messing around with animations or the animator tool, so I wanted to test to see that this animation worked before setting up the "playerWake" animation. Before testing, I also decided to fix my function names as I had been making the first letter lower-case, and I had noticed that all of the built-in functions didn't do that. I don't know-- I like consistency. After making sure that all of my function calls had been modified, I clicked play. That's when I found that not only did the animation not play, but the player control would no longer switch and I'd get the error posted above.

    Can you see any reason why FindGameObjectWithTag would suddenly stop working? I tried deleting my players from the scene in the hierarchy, and re-adding them from my the prefab folder, but that didn't help. I Ctrl-Z'd the hell out of my scripts to try to undo all of my most-recent changes, but that didn't help either.

    I don't know if it matters, but the two players in the scene are "in" the CycleManager game object in the hierarchy. I'm not sure what the correct terminology is for that, but in any case, that's how I had them before so it should still be working.

    EDIT: I made an interesting discovery by printing the array length in debug immediately after the line: players = GameObject.FindGameObjectsWithTag("Player");

    First, it said 14, which is insane because there are only two objects in the scene with the "Player" tag. I tried removing the players from the scene (by removing them from the hierarchy) and the debug said there were 12 objects, which is consistent, at least. Finally, I added the two players back (again, from the prefabs) and tested again. This time, it said there were 16 objects in the array. Whaaaaat!?

    EDIT 2: I noticed that my Unity project always seems to have an asterisk in the title at the top of the window which I think is supposed to indicate that changes have been made since the last save. Strangely, even after I save (with Ctrl+S), the asterisk remains. Is this a bug? Anyways, I wanted to close and re-launch Unity just to see if it would help, but first I saved a copy of my scene. After closing Unity, I opened up the copy and was delighted to find that it wasn't having any problems. I opened the original scene and that one was working too.

    Sorry for the colossal post. I really hope somebody can give me an idea of what's going on though.
     
    Last edited: Oct 21, 2014