I am new to Unity and am working My way through the tutorials. I finished the Space shooter once My way and again following the directions to the letter. In both cases, the game ended as soon as I shot the enemy ship.It does not help that I do not know C# and am learning as I go. I have come to the conclusion that either the sample code is bad OR that the code does not work well with Unity 5.5
Here is the GameController:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour {
public GameObject[ ] hazards;
public Vector3 spawnValues;
public int hazardsCount;
public float spawnWait;
public float startWait;
public float waveWait;
public GUIText scoreText;
public GUIText RestartText;
public GUIText GameOverText;
private bool gameOver;
private bool restart;
private int score;
private int levelCount;
void Start()
{
gameOver = false;
restart = false;
levelCount = 3;
RestartText.text = “”;
GameOverText.text = “”;
score = 0;
UpdateScore();
StartCoroutine (SpawnWaves ());
}
void Update()
{
if (restart)
{
if (Input.GetKeyDown (KeyCode.R))
{
SceneManager.LoadScene(SceneManager.GetActiveScene ().buildIndex);
}
}
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds(startWait);
while(true)
{
while (levelCount <= hazardsCount)
{
levelCount += 2;
for (int i = 0; i < levelCount; i++) {
GameObject hazard = hazards [Random.Range (0, hazards.Length)];
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 = score + newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
}
public void GameOver ()
{
GameOverText.text = “Game Over!!”;
gameOver = true;
}
}
Here is the DestoryByContact:
using UnityEngine;
using System.Collections;
public class DestoryByContact : MonoBehaviour
{
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
private GameController gameController;
private GameController Enemy;
void Start ()
{
GameObject gameControllerObject = GameObject.FindGameObjectWithTag (“GameController”);
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent ();
}
if (gameController == null)
{
Debug.Log (“Cannot find ‘GameController’ script”);
}
}
void OnTriggerEnter (Collider other)
{
if (other.tag == “Boundary” || other.tag == “Enemy”)
{
return;
}
if (explosion != null)
{
Instantiate(explosion, transform.position, transform.rotation);
}
if (other.tag == “Player”)
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
gameController.GameOver();
}
gameController.AddScore(scoreValue);
Destroy (other.gameObject);
Destroy (gameObject);
}
}