Errors Script C# Unity3d 5.2.0f3! Errors with Rigidbody2d

Asked

Viewed 622 times

0

I have a problem with a C# code that I am using to make a game in Unity3d Version 5.2.0f3

This Code I picked up on a video of Playing with Nils ( Youtube channel that teaches how to develop games.

There’s an error in setting the player commands, especially now that unity3d has updated and is using Visual Studio 2015 (I don’t know how to use it).

After creating a code in C# to use in my player, presented me the following errors:

Este é o VS com o codigo aberto

This is the Code :

using UnityEngine; // Obrigado por avisar desta linha gamesinsanity
public class Player : MonoBehaviour
{
// The force which is added when the player jumps
// This can be changed in the Inspector window
public Vector2 jumpForce = new Vector2(0, 280);

// Update is called once per frame
void Update()
{
    // Jump
    if (Input.GetKeyUp("space"))
    {
        Rigidbody2D.velocity = Vector2.zero;
        Rigidbody2D.AddForce(jumpForce);
    }
    }
    }
  • 1

    The error is self-descriptive but to give a solution would need to see other snippets of code. Maybe by watching the video we can help but it would be difficult for us to have to see it to solve your problem. You would probably have to be using a variable with an instance of an object instead of Rigidbody2D which is a type. But I can’t say without seeing more context.

2 answers

2


I can’t see the error image, but one thing I can point out by looking at the code is how to access the Rigidbody2d component. Unity 5 has adjusted its syntax to better align with the scope and visibility concepts of C# (properly object oriented) so where before you would access your Gameobject components as attributes you now need to access with Getcomponent() methods or Getcomponents(). You can find more information on how to properly use component access in the documentation (videos and references)

Briefly, if you have Rigidbody2d attached to the same object as your script you can simply replace the call Rigidbody2D for GetComponent<RigidBody2D>(). But I advise you to take a look at the documentation to understand the correct use of this method even to avoid performance problems since it does a search and not a direct access to the component you should keep a reference in your script instead of performing the search every time that need to use.

If this component is in a parent object or child of the object where the script is located the way to access it will change or even the search method so it is important to look at the documentation and understand what is being done ;)

  • Thank you very much !

  • Bruno notes that in the original answer the method was without typing because I had not put in the code block, I have already edited and adjusted. It is necessary to pass the type you want to find in the method between <>

1

You are trying to access the Object Class and not the object created in memory to be used. To use an object of type rigidbody2D, you need to have it somewhere as a reference, I advise to do this way.

First create a Rigidbody2d object, then in Start* I take the reference of it that already exists in the created object, and inside Update I use the object I created that has the reference to Rigidbody2d.

public class Player : MonoBehaviour
{

    public Vector2 jumpForce = new Vector2(0, 280);
    private Rigidbody2D rigidbody2D;

    void Start()
    {
         this.rigidbody2D = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
       // Jump
       if (Input.GetKeyUp("space"))
       {
           this.rigidbody2D.velocity = Vector2.zero;
           this.rigidbody2D.AddForce(jumpForce);
       }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.