Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I tried to create a climb script and found some similar code. I would like to climb with my player on a ladder. I came up with the following:

bool canClimb = false; float speed = 1;

GameObject player;                          // Reference to the player GameObject.


void Awake () {
    // Setting up the references.
    player = GameObject.FindGameObjectWithTag ("Player");
}



void OnTriggerEnter2D(Collider2D other) { // send in the other collider (should always work)

    if (other.gameObject.tag == "Player") { // used a tag to ID collider as player
        canClimb = true;
        player.rigidbody.useGravity = false;

    }
}

void OnTriggerExit2D(Collider2D other) { // send in the other collider (should always work)

    if(other.gameObject == "Player"){
        canClimb = false;
        other.attachedRigidbody.useGravity = true;

    }
}

void Update () {

    if(canClimb == true){


        if(Input.GetKey(KeyCode.Z)){

            player.transform.Translate (Vector3(0,1,0) * Time.deltaTime*speed);

        }
        if(Input.GetKey(KeyCode.S)){
            player.transform.Translate (Vector3(0,-1,0) * Time.deltaTime*speed);


        }
    }
}

The above code will give a red mark on the useGravity in the line of player.rigidbody.useGravity. I doesn't know how to fix this.

share|improve this question

closed as off-topic by Byte56 Mar 25 at 21:43

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Byte56
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
What have you tried? –  bummzack Mar 25 at 19:46
    
@bummzack, I update my answer with the script and detailed question –  Caspert Mar 25 at 19:57
    
Which version of Unity are you using? Unity 5 just came out, and they changed the api for a call like object.rigidbody –  jhocking Mar 25 at 20:09
    
Yes, I use Unity5. Where should I look for? –  Caspert Mar 25 at 20:16

1 Answer 1

up vote 2 down vote accepted

Unity 5 just came out, and they changed the API for a call like object.rigidbody

Specifically, every component shortcut other than object.transform has been removed, since the Transform component is the only one that all objects have. All the other shortcuts only applied a fraction of the time.

Now you need to access the Rigidbody component using GetComponent(), like so (C# code):

Rigidbody body = player.GetComponent<Rigidbody>();
body.useGravity etc.
share|improve this answer
    
I thought that useGravity doesn't work on a 2D object, am I correct? –  Caspert Mar 25 at 20:28
    
oh these are 2D objects, I missed that. I don't see why useGravity wouldn't work there too, but it's going to be Rigidbody2D instead and that may have a different API than Rigidbody. –  jhocking Mar 25 at 21:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.