无法将字符串统一转换为ui文本 [英] Can't convert string to ui text in unity

查看:108
本文介绍了无法将字符串统一转换为ui文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决方案,但到目前为止没有任何效果,这是我的代码:

I have looked around for solutions but nothing has worked so far, here is my code:

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

public class BirdController : MonoBehaviour 
{
    public float jumpVelocity;
    public Text jumpCooldownDisplayText = "";
    public float speed = 18;
    private Rigidbody rb;
    public float jumpCooldown = 0f;

    // Use this for initialization
    void Start ()
    {
        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void FixedUpdate ()
    {
        jumpCooldown -= Time.deltaTime;
        jumpCooldownDisplayText = Mathf.Round (jumpCooldown).ToString();

        if (Input.GetButtonDown ("Jump") && jumpCooldown <= 0) 
        {
            rb.velocity = Vector3.up * jumpVelocity;
            jumpCooldown = 0.5f;
        }

        float hAxis = Input.GetAxis("Horizontal");
        float vAxis = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(hAxis*speed, 0, vAxis*speed) * Time.deltaTime;

        rb.MovePosition(transform.position + movement);
    }
}

我得到的错误是无法隐式转换类型string' to UnityEngine.UI.Text'".我将此错误放入代码的不同位置.

The error I'm getting is "Cannot implicitly convert type string' toUnityEngine.UI.Text'". I'm getting this error into different places of the code.

推荐答案

UI.Text是UI文本组件,而不是关联的字符串.您需要的是UI Text的text属性:

UI.Text is a UI text component, not the associated string. What you want is the text property of the UI Text:

jumpCooldownDisplayText.text = Mathf.Round (jumpCooldown).ToString();

这篇关于无法将字符串统一转换为ui文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆