Knightingale
Knightingale

Reputation: 11

UI Text Problems

I'm more or less new to Unity, and I'm struggling with the new UI stuff in 4.6. Essentially what I'd like to do is everytime the player takes a move action by pressing w/a/s/d, I'd like the number of turns elapsed to increase by 1, and be reflected in the UI. This is what I have as a character movement controller script right now:

public float speed;
public static int Turns;

void Update() 
{
    if (Input.GetKey(KeyCode.W)&& rigidbody2D.velocity.y == 0.0f && rigidbody2D.velocity.x == 0.0f ) 
    {
        rigidbody2D.AddForce(Vector2.up * speed); 
        Turns = Turns + 1;
    }

    if (Input.GetKey(KeyCode.A)&& rigidbody2D.velocity.y == 0.0f && rigidbody2D.velocity.x == 0.0 ) 
    {
        rigidbody2D.AddForce(-Vector2.right * speed);
        Turns = Turns + 1;
    }

    if (Input.GetKey(KeyCode.S)&& rigidbody2D.velocity.y == 0.0f && rigidbody2D.velocity.x == 0.0) 
    {
        rigidbody2D.AddForce(-Vector2.up * speed);
        Turns = Turns + 1;
    }

    if (Input.GetKey(KeyCode.D)&& rigidbody2D.velocity.y == 0.0f && rigidbody2D.velocity.x == 0.0) 
    {
        rigidbody2D.AddForce(Vector2.right * speed);
        Turns = Turns + 1;
    }
}

And this is the script I'm trying to use to change the text to be the number of turns, but despite there being no highlighted errors and such, Unity won't consider it a valid script to attach to the UI Text object:

public class TurnManager : MonoBehaviour
{
    public Text text;

    void Awake()
    {
        text = GetComponent <Text> ();
    }

    void Update()
    {
        text.text = PlayerController.Turns.ToString();
    }
}

This is kind of what I gathered from a tutorial, but I feel like it's really wrong, haha. Can anyone help me figure out what I'm supposed to have there to make this work?

Upvotes: 1

Views: 146

Answers (2)

Uri Popov
Uri Popov

Reputation: 2167

Are the class name and the file name the same ? Otherwise Unity will just say that the script does not exist or something. This happens if you create a script from the editor and then change the class name from mono develop or visual studio. Best solution is to delete the script and make it again with the correct name from the editor.

Upvotes: 1

codingadventures
codingadventures

Reputation: 2952

Your code is correct, you only need to make a few changes. I assume the first script you posted is the PlayerController. Be careful if you have attached a rigidbody2D to the GameObject in your hierarchy, if you don't set the Gravity Scale to 0 your rigidbody2D.velocity.y will never be zero as the object will start to fall (unless it is on a Collider).

However in you TurnManager you have to set the Text property from the UI. Look at the pic below:

PlayerController

You need to set on your TurnManager script the Text UI object. Code for your TurnManager will be simply:

using UnityEngine;
using UnityEngine.UI;

public class TurnManager : MonoBehaviour 
{
    public Text text;

    void Update()
    {
        text.text = PlayerController.Turns.ToString();
    }
}

Once you do this changes you will correctly read the number of Turns from the PlayerController script.

Hope it helps.

Upvotes: 1

Related Questions