2
votes

I have the following problem, and I'd appreciate greatly if someone could help.

I've been trying create a roll-a-ball game in Unity, similar as the one presented in one of the tutorials on their official website, but with one difference: I'd like to add GUI text that would show the score. Every time the ball hits (and destroys) one of the rotating cubes, the score should increase by 1, and the text should show the current score.

I've watched the following tutorial:

http://unity3d.com/learn/tutorials/projects/space-shooter/counting-points

I have created the UI text, called ScoreText, and, on the other hand, I have written the script that makes an instance of GUIText class, called guitext, show the current score. However, I cannot connect my particular GUI text (ScoreText) with the script, i.e. I cannot tell the script to use that particular GUI text to show score. In the video, at 16:35, they just drag the GUI text to the instance of the GUIText class from the script (in my case, guitext), in order to tell the script it's that particular text that should show the score. But when I try to drag it, I simply can't do it. I have no idea why.

I don't know if this could cause the problem: under "Create" in the free version of Unity, I could not find anything called "GUI text" but instead I created a UI text. I know it's the same thing, but... In the script, I define an instance of GUIText class - guitext - so maybe, when I try to add my UI text to it, it won't work because my text really doesn't belong to the GUIText class?

4
Welcome to Stack Overflow! You might want to take what you posted and distill it into something a bit more concentrated. Imagine you're writing an e-mail to your coworker. It is very hard for us to read paragraphs and paragraphs and figure out what you want. Give a couple of sentences of background, explain what you need to, then state your question explicitly. That way, it'll be much easier for us to help you.Alex K
Upload your code so that I can see where the problem is.Programmer
What version of unity are you using? I don't believe GuiText and UI Text are the same thing, hence why you can't drag it into the inspector variable...Savlon

4 Answers

2
votes

Since you are using Unity 4.6 which has a new GUI system and not the Legacy UI which was present when the tutorial which you mentioned was made. There are two options - either you create an empty gameobject and add add a GUIText component to it. You can add the GUIText to empty gameobject by selecting it in the Hierarchy which should display the objects properties in the Inspector panel.

In the Inspector, you should see an Add Component button, click on it and search for GUIText. Add it and that should solve your problem.

Oh yea, the second option is to migrate to Unity 4.6 new GUI, which should take time unless you are pretty good with unity.

If you want to learn Unity 4.6 GUI, you can refer these links

Unity Official

Unity 4.6 tutorials by TheGameContriver

1
votes

I am only posting on this because many come to this one and get lost. Here is how to make this work in Unity5.3 at the time of this response. This is in C#

Simply use the GameController game object to attach the script to. Second create a UI text. Toy only call it score or scoretext for your own referral otherwise the name itself does not matter.

using UnityEngine;
using UnityEngine.UI;///needed to reference the new UI setup in this script type
using System.Collections;/// <summary>
/// allows collections of related information. 
/// </summary>


public class GameController : MonoBehaviour { 

public GameObject targets;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;

public Text Text; /// <summary>
/// change this because you dont actually use the words UI or GUI. Thats just for the reference and not clarified by instructors. 
/// </summary>
private int score;

void Start()
{
    score = 0;
    UpdateScore();
    StartCoroutine(SpawnWaves());
}

IEnumerator SpawnWaves()///this depends on the above using System.Collections
{
    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(targets, spawnPosition, spawnRotation);
            yield return new WaitForSeconds(spawnWait);
        }
        yield return new WaitForSeconds(waveWait);
    }
}

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

void UpdateScore()
{
    Text.text = "Score: " + score;
}
}
1
votes

Confirmed. In the GameController.cs file, change the declaration of public GUIText scoreText to public Text scoreText, and then it will let you drag it on the Game Controller as the video shows.

Update your Unity tutorials, peoples! Just spent an hour trying to figure this out!

0
votes

One more thing...check the Z position. My UI looked perfect head on in the scene view, but my text never showed in the game...then I rotated the scene view and found my text was way behind the camera. Seems when you add something to the UI, the z value is somewhat random.