Player suddenly not registered by anything?
This is a bit of a long one so bear with me. Any help would be immensely appreciated. This is for a school project.
So I was working on trying to find a way to get the player's death sound to play after the game is paused using Time.timeScale = 0. I couldn't figure out how to get it to work, so I deleted that code. Now, suddenly, everything related to the player is completely broken. I have pored over each and every line of every script in my game, and cannot seem to find the issue. I even took a break, went outside, had some dinner, came back and did it again. I simply cannot figure it out.
Important info:
1) The player object is tagged as "Player". It is not deactivated and is currently in the scene.
2) The player bullet object is tagged as "PlayerBullet."
3) The player, the player's bullets, and the enemies all have RigidBodies and colliders attached. None of them are checked as triggers or kinematic.
4) The script that controlled the enemies looking at the player was not changed.
5) Every GameObject in the scene was placed there from their prefabs, with the scripts in place and all necessary fields filled with the necessary items. I checked each one individually just to make sure.
What I have found:
1) I have two scripts that work off of two of the player's values, health and shield, which update the image in the canvas. They reference these values from a script that is attached to the player using FindObjectWithTag("Player"), and then they access its health or shield variable. They are suddenly throwing a NullReferenceException.
2) I had a script that made the enemies always look at the player in the Update section using transform.LookAt (player.transform). They now face the opposite direction of their prefab when placed, when originally they followed the prefab's values. There is no error thrown. I tried to use Debug.Log to tell me if the player actually existed in the script's eyes, and it gave a "yes" even when I removed the player from the scene.
3) These enemies would shoot at the player by following the player's transform, but now shoot in a straight line in the same direction they are facing as they are not turning.
4) The player's bullets used to ricochet off of the enemy once hit, but now go straight through it as though it was not there.
5) The enemies used to die after two hits and play an explosion sound. At first when everything went haywire, the enemies would suddenly be killed in one shot instead of two. It may be that the bullet was going through them instead of ricocheting, everything hadn't broken yet so I wasn't paying complete attention to that. They both had their health set to 2 and this field was not altered. Both enemies behaved in the exact same way after testing this several times. Now the bullets do not affect the enemies so I cannot test this anymore.
6) The script that adjusted the enemy health did so by finding a collision with object tagged "PlayerBullet." Since the bullets have started going through the enemies, this script is no longer being activated like it should. The enemies do not die at all anymore. 6) I tried changing the health and shield canvas object to use the Find("Player") instead of FindObjectsWithTag, but it still did not work.
Tldr;
Nothing seems to be registering the objects tagged "Player" or "PlayerBullet" anymore, and any physics collisions involving the player or bullets are suddenly broken even with the RigidBodies and colliders in place. I have tried removing it and placing it back into the scene. The player's movement and shooting works fine, but the game is simply acting like the player and their bullets are no longer there. The healthbars cannot reference the player object's health, and the enemies cannot be shot by or look at the player, even though it is right there, tagged and everything. All of this worked fine up until I tried to mess with stopping Time and adding a sound.
Before blasting a million lines at code and asking that people sift through it all, is there anything I could try that I have not thought of?
Here are the two bits of code that are specifically not working......
Health meter's script used to reference the player (which was untouched and now throws NullReference):
GameObject player = GameObject.FindGameObjectWithTag ("Player");
currentHealth = player.GetComponent ();
switch (currentHealth.health) { <------ this is where the exception points to
Enemy's look at script (which was untouched and now does nothing):
void Start () {
InvokeRepeating ("Shoot", 2.0f, 1.0f);
player = GameObject.FindWithTag ("Player");
}
void Update () {
transform.LookAt (player.transform);
}