Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Space Shooter UI - Counting Points Tutorial Fix

Discussion in 'Community Learning & Teaching' started by Webbstre, Mar 24, 2015.

  1. Webbstre

    Webbstre

    Joined:
    Mar 7, 2015
    Posts:
    41
    I just thought I would share my fix (which took hours of googling without result and trying things) to fix the code for Unity5 to work for the Space Shooter's points system. Here is my GameController.cs in C# *

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI; //Added
    4.  
    5. public class GameController : MonoBehaviour
    6. {
    7.     public GameObject hazard;
    8.     public Vector3 spawnValues;
    9.     public int hazardCount;
    10.     public float spawnWait;
    11.     public float startWait;
    12.     public float waveWait;
    13.  
    14.  
    15.    private Text scoreText; // Changed to Private
    16.  
    17.     private int score;
    18.  
    19.     void Start ()
    20.     {
    21. //Begin Tag code here.
    22.         GameObject scoreTextObject = GameObject.FindWithTag ("ScoreText");
    23.        
    24.         if (scoreTextObject != null) {
    25.             scoreText = scoreTextObject.GetComponent<Text>();
    26.         }
    27.         if (scoreText == null) {
    28.             Debug.Log("Cannot Find 'scoreText' Script");
    29.         } //Ended here
    30.  
    31.         score = 0;
    32.         UpdateScore ();
    33.         StartCoroutine (SpawnWaves ());
    34.     }
    35.  
    36.     IEnumerator SpawnWaves ()
    37.     {
    38.         yield return new WaitForSeconds (startWait);
    39.         while (true)
    40.         {
    41.             for (int i = 0; i < hazardCount; i++)
    42.             {
    43.                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
    44.                 Quaternion spawnRotation = Quaternion.identity;
    45.                 Instantiate (hazard, spawnPosition, spawnRotation);
    46.                 yield return new WaitForSeconds (spawnWait);
    47.             }
    48.             yield return new WaitForSeconds (waveWait);
    49.         }
    50.     }
    51.  
    52.     public void AddScore (int newScoreValue)
    53.     {
    54.         score += newScoreValue;
    55.         UpdateScore ();
    56.     }
    57.  
    58.     void UpdateScore ()
    59.     {
    60.         scoreText.text = "Score: " + score;
    61.     }
    62. }
    The changes are commented.. The only other step you need is to create a ScoreText tag and add it to the Score Text object within the editor. I tried every other suggestion I could find via google and I always at least ended up with an error with the way scoreText was defined, and I couldn't assign anything to it within the editor. Instead I tried the earlier tag trick to fix it.
     
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Thanks for sharing!

    The solution is, however, on the video as annotations. GUIText still exist. You simply need to add the component manually. After that the tutorial works as written.

    You can also use the UI Text, and the only change you need to make to the script is add:
    Code (csharp):
    1. using UnityEngine.UI;
    ... at the top and then change the GUIText references to Text references.

    The rest of the code should work as is.

    There is a space shooter thread in the stickies with more details.
     
    ddyson1 likes this.
  3. jhiramis

    jhiramis

    Joined:
    Feb 11, 2018
    Posts:
    4
    You may also want to make the make a new function for game controller: see the codes below for "DestroyByContact":

    public GameObject explotion;
    private GameObject newProjectile;
    public GameObject playerExplotion;
    public int scorePoints;

    void gameController_Main()
    {
    GameController gameController = new GameController();
    gameController = GameObject.FindObjectOfType<GameController>();
    GameObject gameControllerObject = GameObject.FindWithTag("GameController");
    if (gameController != null)
    {
    gameController = gameControllerObject.GetComponent<GameController>();
    }
    if (gameController == null)
    {
    Debug.Log("Cannot Find 'GameController' script");
    }
    gameController.AddScore(scorePoints);

    }
    ********************************************************************************************************************
    //And declare it on void OnTriggerEnter(Collider other)
    *********************************************************************************************************************

    void OnTriggerEnter(Collider other)
    {
    if (other.tag == "Boundary")
    {
    return;
    }
    newProjectile = Instantiate (explotion, transform.position, transform.rotation) as GameObject;
    if (other.tag == "Player")
    {
    newProjectile = Instantiate(playerExplotion, other.transform.position, other.transform.rotation) as GameObject;

    }
    gameController_Main(); //this the declaration of the function gameController_Main
    Destroy(other.gameObject);
    Destroy(gameObject);

    }




    for the code in "GameController" make that:



    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;


    public class GameController : MonoBehaviour {


    public GameObject hazard;
    public Vector3 SpawnValue;
    public int hazardCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;

    private GUIText scoreText; //make the GUIText as private
    private int score;

    void Start ()
    {
    GameObject scoreTextObject = GameObject.FindWithTag("ScoreText");

    if (scoreTextObject != null)
    {
    scoreText = scoreTextObject.GetComponent<GUIText>();
    }
    if (scoreText == null)
    {
    Debug.Log("Cannot Find 'scoreText' Script");
    }

    score = 0;
    UpdateScore();
    StartCoroutine(SpawnWaves());
    }

    IEnumerator SpawnWaves ()
    {
    yield return new WaitForSeconds (startWait);
    while (true)
    {
    for (int i = 0; i < hazardCount; i++)
    {
    Vector3 spawnPosition = new Vector3 (Random.Range (-SpawnValue.x, SpawnValue.x), 0.0f, SpawnValue.z);
    Quaternion spawnRotation = Quaternion.identity;
    Instantiate (hazard, spawnPosition, spawnRotation);
    yield return new WaitForSeconds (spawnWait);
    }
    yield return new WaitForSeconds(waveWait);
    }
    }

    public void AddScore(int newScoreValue)
    {
    score += newScoreValue;
    UpdateScore();
    }

    void UpdateScore ()
    {
    scoreText.text = "Score: " + score;
    }

    }

    ************************************************************************************************************

    Hope this will help.
     
  4. ratthorley

    ratthorley

    Joined:
    Aug 8, 2018
    Posts:
    1
    Thank you Webbstre - I had been struggling with this for days.

    The only thing missing is the code for the restart and game over text objects, so I have added the complete revised GameController class below. You will also need to create the tags in the Inspector for these text objects in the same way as the score text.

    using UnityEngine;
    using System.Collections;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI; //Added

    public class GameController : MonoBehaviour
    {
    public GameObject hazard;
    public Vector3 spawnValues;
    public int hazardCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;
    public Text restartText;
    public Text gameOverText;
    private Text scoreText; // Changed to Private
    private bool gameOver;
    private bool restart;
    private int score;

    void Start()
    {

    gameOver = false;
    restart = false;

    //Begin Tag code here.

    GameObject scoreTextObject = GameObject.FindWithTag("ScoreText");

    if (scoreTextObject != null)
    {
    scoreText = scoreTextObject.GetComponent<Text>();
    }
    if (scoreText == null)
    {
    Debug.Log("Cannot Find 'scoreText' Script");
    }

    GameObject restartTextObject = GameObject.FindWithTag("RestartText");

    if (restartTextObject != null)
    {
    restartText = restartTextObject.GetComponent<Text>();
    }
    if (restartText == null)
    {
    Debug.Log("Cannot Find 'restartText' Script");
    }

    GameObject gameOverTextObject = GameObject.FindWithTag("GameOverText");

    if (gameOverTextObject != null)
    {
    gameOverText = gameOverTextObject.GetComponent<Text>();
    }
    if (gameOverText == null)
    {
    Debug.Log("Cannot Find 'gameOverText' Script");
    }

    score = 0;
    UpdateScore();
    StartCoroutine(SpawnWaves());
    }

    void Update()
    {
    if (restart)
    {
    if (Input.GetKeyDown(KeyCode.R))

    SceneManager.LoadScene(SceneManager.GetActiveScene().name);

    }
    }

    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 = "Restart (R)";
    restart = true;
    break;
    }
    }
    }

    public void AddScore(int newScoreValue)
    {
    score += newScoreValue;
    UpdateScore();
    }

    void UpdateScore()
    {
    scoreText.text = "Score: " + score;
    }

    public void GameOver()

    {
    gameOverText.text = "Game Over!";
    gameOver = true;

    }
    }


    I hope this helps others who have been struggling.