我正在按照youtube上的教程在Unity中制作游戏
这是视频的链接,它将在他编写此代码Link
时开始当我进入另一个房间时,它只是显示和消失的文本 如果需要文本,则OnTriggerEnter2D调用StartCoroutine
屏幕截图 Pic
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RoomMove : MonoBehaviour
{
public Vector2 cameraChange;
public Vector3 playerChange;
private CameraMovment cam;
public bool needText;
public GameObject text;
public Text placeText;
public string placeName;
// Start is called before the first frame update
void Start()
{
cam = Camera.main.GetComponent<CameraMovment>();
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("Player"))
{
cam.minPosition += cameraChange;
cam.maxPosition += cameraChange;
collision.transform.position += playerChange;
if(needText)
{
StartCoroutine(placeNameCo);
}
}
}
private IEnumerator placeNameCo()
{
text.SetActive(true);
placeText = placeName;
yield return new WaitForSeconds(4f);
text.SetActive(false);
}
}
我遇到2个错误:
Error CS1503 Argument 1: cannot convert from 'method group' to 'string' Assembly-CSharp
Error CS0029 Cannot implicitly convert type 'string' to 'UnityEngine.UI.Text' Assembly-CSharp
答案 0 :(得分:0)
placeText
是整个Text Component,所以您想将字符串分配给它的text
property,例如:
placeText.text = placeName;
此外,StartCoroutine需要参数中的例程或字符串。因此,将第38行替换为:
StartCoroutine(placeNameCo())
或StartCoroutine("placeNameCo")
首选第一个,因为字符串版本启动协程的运行时开销较高,并且您只能传递一个参数。