簡體   English   中英

AddComponent(“ Rigidbody”)或其他組件不起作用

[英]AddComponent(“Rigidbody”) or another component doesn't work

我正在嘗試編寫一個非常簡單的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
 }

我不知道為什么會這樣。 任何願意幫助我的人,我將一如既往地感謝您!

這個

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線也一樣

AddComponent是部分GameObject ,而不是你WeaponPickUp

使用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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM