Badtrap
Badtrap

Reputation: 99

AddComponent("Rigidbody") or another component doesn't work

I am trying to program a very simple FPS game and I reached a point where I need to create a pickup weapon system. In order to finish that system, I am stuck on a point where I need to AddComponent("Rigidbody") and AddComponent("BoxCollider") and Unity3D throws that error:

'AddComponent is not a member of 'WeaponPickUp'

Where WeaponPickUp is my Javascript script file.

Below is my code:

 #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
 }

I've no idea why this happens. Anybody willing to help me out, I will appreciate it as always!

Upvotes: 1

Views: 1741

Answers (2)

Programmer
Programmer

Reputation: 125445

Remove this keyword with gameObject then remove as Rigidbody and as BoxCollider

This is what it should look like:

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

The syntax above should have worked but it is obsolete. It changed in Unity 5. You will get error if you do it like that. Below is the new syntax and right way to do it now.

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

Your whole code:

#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
}

Upvotes: 0

Gunnar B.
Gunnar B.

Reputation: 2989

This

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

needs to be

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

Same for the Destroy lines

AddComponent is part of GameObject, not your WeaponPickUp

Upvotes: 1

Related Questions