错误 CS0411:方法“UnityEngine.Object.FindObjectOfType()”的类型参数
本文关键字:类型参数 FindObjectOfType CS0411 方法 Object UnityEngine 错误 | 更新日期: 2023-09-27 18:35:38
这是完整的错误:
资产/脚本/框启动器.cs(15,32):错误 CS0411:类型参数 对于方法'UnityEngine.Object.FindObjectOfType()'不能 从使用情况推断。尝试显式指定类型参数
这是我的代码:
using UnityEngine;
using System.Collections;
public class BoxLauncher : MonoBehaviour {
public GameObject[] boxPrefabs;
public float fireDelay = 3f;
public float nextFire = 1f;
public float fireVelocity = 10f;
void FixedUpdate () {
// This is the line that the error is pointing to.
if (GameObject.FindObjectOfType().hasLost){
return;
}
nextFire -= Time.deltaTime;
if(nextFire <= 0) {
// Spawn a new box!
nextFire = fireDelay;
GameObject boxGO = (GameObject)Instantiate(
boxPrefabs[ Random.Range(0, boxPrefabs.Length)],
transform.position,
transform.rotation
);
boxGO.rigidbody2D.velocity = transform.rotation * new Vector2(0, fireVelocity);
GameObject.FindObjectOfType<ScoreManager>().score++;
}
}
}
GameObject.FindObjectOfType() 需要对象类型才能查找。您没有定义和对象类型? 尝试添加诸如 GUItexture 之类的类型。
代码如下所示:GameObject.FindObjectOfType(typeof(GUITexture))
而不是 : GameObject.FindObjectOfType();