gpt4 book ai didi

c# - 销毁对象时出错

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:17 24 4
gpt4 key购买 nike

public class NewBehaviourScript : MonoBehaviour 
{
private string hitobject;

void Update () {
if (Input.GetButtonUp ("Jump")) {
Debug.Log("SPACE");
Rigidbody r = GameObject.Find("trigger").AddComponent<Rigidbody>();
r.mass = 3f;
}
}
void OnCollisionEnter(Collision myCollision)
{
hitobject = myCollision.gameObject.tag;
if (hitobject == "wall")
{
Destroy(hitobject);
}
}
}

有什么问题吗?

错误说:

Assets/NewBehaviourScript.cs(20,25): error CS1502: The best overloaded method match for `UnityEngine.Object.Destroy(UnityEngine.Object)' has some invalid arguments

Assets/NewBehaviourScript.cs(20,25): error CS1503: Argument #1'
cannot convert
string' expression to type `UnityEngine.Object'

最佳答案

您正在尝试销毁标签而不是游戏对象。

你可以这样修复它:

void OnCollisionEnter(Collision myCollision)
{
GameObject g = myCollision.gameObject;

if(g.tag == "wall")
Destroy(g);
}

关于c# - 销毁对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26072289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com