This is where I’m so far the game doesn’t produce the game over message nor the Press ‘R’ for restart.
Hi Martin,
This is a bit too lengthy to go through today, family etc, but the reason I asked you to put the Debug.Log
statement in your code above where you already had the if
statement was to diagnose the issue.
At the moment your code is saying “are there more than 13 enemies, ok, great, destroy one”, but at some point that is going to fail, e.g. When you have destroyed enough and there is only 13 left, then that if
statement won’t return true and your Destroy
method will not be called. The enemy ship will be left in place.
This is the behaviour you described you had at the beginning and I was trying to get you to do a bit of a diagnosis so that you would see, perhaps, that you never have more than 13 enemies, or maybe at some point you only have 13 enemies, in both cases the Destroy
method won’t be called.
I believe you have a GameController GameObject and class, this would be the ideal object to be monitoring the count of enemies in the game. At the start of the game it should run off and return the count of the enemies that are perhaps tagged, or, you could find them by type if they have a class on them. The Game Controller now has a variable storing the total number of enemies in the scene.
When one of your enemies gets destroyed you then need to reduce that count by one. At the point of reducing this count you should also check to see if there are any left. If the count of all enemies in the scene is now zero then trigger the game iver condition, for now that could just be a Debug.Log
statement if you are still having problems with the UI.
As part of this, you could also consider the ability to increase the number of enemies that are in the scene. Perhaps instead of searching the scene for all of the enemies that are tagged, if you have an enemy script on each enemy, in its Awake
method it could communicate with the Game Controller and call a method that increases the number of enemies by one. Each enemy would then notify the GameController at the start of the game.
On a side note, if you could apply the code formatting characters when copy/pasting your code into the forum too that would be helpful as it makes it easier to read.
See also,
- Forum User Guides : How to apply code formatting within your post
I apologize, I forgot this is good family time, I’m having many issues with my own so I stay home like always never the less is a bless day, I talk to who I can in my family and who doesn’t insult me for not having a job yet. I hope to chat with you after the New Year and get this game completed Thank you
No need to apologise Martin, I am dipping in from time to time via my mobile which is why its a bit harder to do the lengthier, more in depth, responses.
I have my son 50% of the time, his mother the other 50%, I fully appreciate the emotional side of this time of year.
Not being employed can be tough, I am currently not employed myself and have also noted how people’s attitudes towards you can change, in my own experience this had affected my confidence and belief that I could still do what I did before.
We all have to travel our own journeys, following a path, but sometimes cut a new one when the existing one does appear to take us where we want to be.
I hope 2019 will be good to you and wish you every success.
Yes it’s hard at times, what makes it harder is the fact the news states unemployment rate is so low I beg to differ at this point. I have sales experience not one company I applied to has called me. I just finished school find it harder to get a job with skills I already have not necessarily in multi media graphic design. I been out of work 1yr and half thus far I always had some type of work painting / maintenance its’s not easy to find at this time.
I’m sure you going to land something with the skills you have. Its not easy being unemployed with a family to support. I starting to get a picture whats happening across the glob .You and your family are in my prayers. It’s lovely to have kids around my sister and cousins kids are pleasant to be around and watch them grow. I’m understanding how hard it is to get good work in this business. I hope my school can help me land something soon applying on my own is not working out for me. I believe companies just can’t not afford skilled employees it may be inexpensive to hire someone with lesser skills for part-time status. I hope the new year brings you and your family good fortune. Thank you
This is what I got done so far but still can get Game Over! or press ® for restart to show or work the You Win! message and You Lose! message don’t show either but they are all hidden when I play the game. Just let me know if I’m on the right track? In debug it actually count enemies. Thank you
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.UI;
using System.Collections;
public class GameController : MonoBehaviour
{
public GameObject hazard;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public Text scoreText;
public Text restartText;
public Text gameOverText;
public Text youWinText;
public Text youLoseText;
[SerializeField]private PlayableDirector masterTimelinePlayableDirector;
[SerializeField]private GameObject playerShip;
private bool gameOver = false;
private bool restart = false;
private bool youWin = false;
private bool youLose = false;
private bool killedAllEnemies = false;
private int score;
private int enemiesLeft = 0;
void Start()
{
enemiesLeft = 13;
restartText.text = "";
gameOverText.text = "";
youWinText.text = "";
youLoseText.text = "";
score = 0;
UpdateScore();
StartCoroutine(SpawnWaves());
}
void Update()
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemies");
enemiesLeft = enemies.Length;
if (Input.GetKeyDown(KeyCode.A))
{
enemiesLeft--;
}
if (enemiesLeft == 0)
{
endGame();
}
}
void endGame()
{
killedAllEnemies = true;
}
void OnGUI()
{
if (killedAllEnemies)
{
GUI.Label(new Rect(0, 0, 200, 20), "all gone");
}
else
{
GUI.Label(new Rect(0, 0, 200, 20), "Enemies Remaining : " + enemiesLeft);
}
if (restart)
{
if (Input.GetKeyDown(KeyCode.R))
{
Invoke("LoadFirstScene", 2f);
}
}
}
IEnumerator SpawnWaves()
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
if (gameOver)
{
restartText.text = "Press 'R' for Restart";
restart = true;
break;
}
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
void UpdateScore()
{
scoreText.text = "Score: " + score;
}
public void YouWin()
{
youWinText.text = "You Win!";
youWin = true;
}
public void YouLose()
{
youLoseText.text = "You Lose!";
youLose = true;
}
public void GameOver()
{
gameOverText.text = "Game Over!";
gameOver = true;
masterTimelinePlayableDirector.Stop();
playerShip.SetActive(false);
}
}
These are the errors I receive:
HI Martin,
I’ve got a little bit of time on my hands today, so if you would like to share your project files again, as they are at this time, but without the library directory I will be happy to take another quick look.
Lets try to resolve some specifics though, so, from my current understanding, you want;
- to be able to count the number of enemies in the scene
- to be able to reduce the number of counted enemies as they are destroyed by the player
- to display the Game Over UI objects when;
- the enemy count reduces zero
- the player is killed
Anything other than that at this time?
Hi Rob, Thank you I zipping will post when it’s done. I hope your holiday spent with your wife and child was pleasant ?
Downloading now, will take a look and respond in due course.
Updated Thu Dec 27 2018 16:55
Just to let you know, we appear to be experiencing some broadband issues and the speeds I am getting are fairly awful. Your file is downloading, but painfully slowly. If you don’t see a response for a while it’s not because I’ve forgotten, it’s because I’ve not got the file!
oOhh, No worries pal I hope your speed gets up and running again. I think when I removed the library it didn’t save the you lose and you win message I created. I thank for all you do.
Resolved the networking issues now, have the file, will be taking a look in a sec.
The /library directory is, for all intents and purposes, a copy of your project, so if you had the files/changes in your project they should still be there, e.g. not related to the /library folder.
Okay keep me posted.
Will do.
Thank you
In that long post I did above which had all the steps required to get where I had got to, I had missed something.
I was just looking at your CollisionHandler.cs script and note that you’ve added this;
private GameController gameController;
private void Start()
{
gameController = FindObjectOfType<GameController>();
}
…but nothing else in that script uses it.
Looking at my instructions above, I indicated to make these changes and then moved to the GameController.cs script where we create the GameOver
method - the CollisionHandler.cs script was then meant to call that method but it would seem I forgot to add that instruction for you - my apologies.
So, in your CollisionHandler.cs_ script, make this change;
void OnTriggerEnter(Collider other)
{
StartDeathSequence();
deathFX.SetActive(true);
gameController.GameOver();
}
You can now see the call to the GameOver
method.
Make this one change and you should find that when you crash into an enemy the player ship disappears, the movement along the rail stops, and the “Game Over!” text appears.
Do this before we move on as that will save creating a huge post with multiple instructions.
Also, this code;
void Update()
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemies");
enemiesLeft = enemies.Length;
if (Input.GetKeyDown(KeyCode.A))
{
enemiesLeft--;
}
if (enemiesLeft == 0)
{
endGame();
}
}
in your GameController.cs isn’t going to do what you want it to. Update
is called every frame, but above your keyboard input check you are getting a fresh count of how many enemies you have left, so whilst you deduct one when your press A, the next frame it gets a fresh count to corrects it again.
I’m guessing you are using a GamePad when you play, as an aside, you have the KeyCode.A for this check which is also the same key to move the ship to the left
I would just delete this for now;
if (Input.GetKeyDown(KeyCode.A))
{
enemiesLeft--;
}
Let me know when you’re ready and we’ll move on to the next bit.
Next issue - detecting all enemies are destroyed…
I actually managed to destroy all of the enemy ships when I played this evening, that’s a first!
At the end however it still stated in the top-left corner of the screen that there were two remaining.
I paused the game and looked at the GameObjects which were in the Hierarchy. Your Enemies parent GameObject has had its tag set to “Enemies”, as has the GameController GameObject for some reason. Your code is checking for all GameObjects that have the “Enemies” tag, here are two that, as the player, you will never be able to shoot/destroy.
Remove the “Enemies” tag from these two GameObjects, setting them back to “Untagged”, when you now shoot down all of the enemy ships your count in the top-left corner will say “All Gone”.
Incidentally, I found shooting all of the ships down rather annoying/time-consuming when just trying to test your code, to make it easier I just disabled Waves 2, 3, 4, and 5 and the second ship in Wave 1.
This left just ONE ship to shoot which makes testing a lot easier/quicker.
Okay will do.
Again, let me know when done and working as expected.