【问题标题】:Can't convert string to ui text in unity无法统一将字符串转换为ui文本
【发布时间】:2018-08-06 07:58:35
【问题描述】:

我已经四处寻找解决方案,但到目前为止没有任何效果,这是我的代码:

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' toUnityEngine.UI.Text'”。我将此错误放入代码的不同位置。

【问题讨论】:

    标签: string user-interface unity3d text


    【解决方案1】:

    UI.Text 是一个 UI 文本组件,而不是关联的字符串。你想要的是 UI Text 的 text 属性:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-17
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多