【发布时间】:2021-11-13 03:44:47
【问题描述】:
所以我正在尝试为我的游戏创建一个计分器,并且我希望它每次都增加并且敌人被杀死。但是,当我尝试将分数设置为之前的值 + 1 时,我收到一条错误消息:error CS1503: Argument 1: cannot convert from 'UnityEngine.UI.Text' to 'string'
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShieldController : MonoBehaviour
{
Text score;
bool shot = false;
public void LaunchForward(float speed)
{
shot = true;
this.GetComponent<Rigidbody2D>().velocity = transform.up * speed;
}
void Start() {
score = GameObject.Find("Canvas/Text").GetComponent<Text>();
}
void OnCollisionEnter2D(Collision2D other) {
if (shot) {
Destroy(gameObject);
Destroy(other.gameObject);
score.text = (int.Parse(score) + 1).ToString();
}
}
}
文本在开始时设置为“0”。 为什么会出现此错误?
【问题讨论】:
-
(int.Parse(score.text)...
标签: string unity3d text type-conversion integer