【问题标题】:AddComponent("Rigidbody") or another component doesn't workAddComponent("Rigidbody") 或其他组件不起作用
【发布时间】:2017-05-04 19:48:36
【问题描述】:

我正在尝试编写一个非常简单的 FPS 游戏,并且我需要创建一个拾取武器系统。为了完成该系统,我被困在需要AddComponent("Rigidbody")AddComponent("BoxCollider") 的地方,而 Unity3D 会抛出该错误:

'AddComponent 不是'WeaponPickUp'的成员

WeaponPickUp 是我的 Javascript 脚本文件。

下面是我的代码:

 #pragma strict

 var pickup = false;
 var check = 2;

 function Update () {
     if (Input.GetButtonDown("pickup") && check % 2 == 0){
         GetObject();
         pickup = true;
         check = check + 1;
     }
     else if (Input.GetButtonDown("pickup") && (check % 2 == 1)){
         pickup = false;
         check = check - 1;
         this.AddComponent("Rigidbody") as Rigidbody;
         this.AddComponent("BoxCollider") as BoxCollider;
         this.GetComponent(BoxCollider).enabled = true;
     }
 }

 function GetObject(){
     var position : GameObject = GameObject.Find("weaponPosition");
     this.transform.position = position.transform.position;
     Destroy(GetComponent(Rigidbody));
     Destroy(GetComponent(BoxCollider));
     this.transform.parent = GameObject.Find("FPSController").transform;
     this.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
     //    this.transform.parentposition
 }

我不知道为什么会这样。任何愿意帮助我的人,我会一如既往的感激!

【问题讨论】:

  • Unity抛出错误时,会附带行号,是哪一行给你的错误?

标签: unity3d components unityscript


【解决方案1】:

这个

this.AddComponent("Rigidbody") as Rigidbody;
this.AddComponent("BoxCollider") as BoxCollider;
this.GetComponent(BoxCollider).enabled = true;

需要

gameObject.AddComponent("Rigidbody") as Rigidbody;
gameObject.AddComponent("BoxCollider") as BoxCollider;
gameObject.GetComponent(BoxCollider).enabled = true;

Destroy 行也是如此

AddComponentGameObject 的一部分,而不是您的 WeaponPickUp

【讨论】:

  • AddComponent 和 GetComponent 使用泛型类型。不应该是gameObject.GetComponent<BoxCollider>()
  • @Okomikeruko GetComponent()/AddComponent() 有一个泛型类型版本和一个将字符串名称作为参数的版本。任何一个版本都可以。
  • 是的,我自己比较偏爱泛型类型。省去了必须转换结果的麻烦。
  • 那么如果他使用的是字符串方法,那么他必须在他的字符串周围使用" ",对吧?
【解决方案2】:

gameObject 删除this 关键字,然后删除as Rigidbodyas BoxCollider

它应该是这样的:

gameObject.AddComponent("Rigidbody");
gameObject.AddComponent("BoxCollider");
gameObject.GetComponent("BoxCollider").enabled = true;

上面的语法应该可以工作,但它已经过时了。它在 Unity 5 中发生了变化。 如果你这样做,你会得到错误。下面是新的语法和正确的方法。

GetComponent.<Rigidbody>();
GetComponent.<BoxCollider>();
GetComponent.<BoxCollider>().enabled = true;

你的整个代码:

#pragma strict

var pickup = false;
var check = 2;

function Update () {
    if (Input.GetButtonDown("pickup") && check % 2 == 0){
        GetObject();
        pickup = true;
        check = check + 1;
    }
    else if (Input.GetButtonDown("pickup") && (check % 2 == 1)){
        pickup = false;
        check = check - 1;
        GetComponent.<Rigidbody>();
        GetComponent.<BoxCollider>();
        GetComponent.<BoxCollider>().enabled = true;
    }
}

function GetObject(){
    var position : GameObject = GameObject.Find("weaponPosition");
    this.transform.position = position.transform.position;
    Destroy(GetComponent(Rigidbody));
    Destroy(GetComponent(BoxCollider));
    this.transform.parent = GameObject.Find("FPSController").transform;
    this.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
    //    this.transform.parentposition
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 2023-04-09
    • 2021-10-14
    • 1970-01-01
    • 2021-09-16
    相关资源
    最近更新 更多