Errors Script C # Unity3D 5.2.0f3! Errors with RigidBody2D

0

I'm having a problem with a C # code I'm using to make a game on Unity3D Version 5.2.0f3

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

This is an error when setting the Commands of the player, especially now that unity3d has updated and is using Visual Studio 2015 (I do not know how to play it).

After I created a C # code to use in my player, it introduced the following errors:

ThisistheCode:

usingUnityEngine;//ObrigadoporavisardestalinhagamesinsanitypublicclassPlayer:MonoBehaviour{//Theforcewhichisaddedwhentheplayerjumps//ThiscanbechangedintheInspectorwindowpublicVector2jumpForce=newVector2(0,280);//UpdateiscalledonceperframevoidUpdate(){//Jumpif(Input.GetKeyUp("space"))
    {
        Rigidbody2D.velocity = Vector2.zero;
        Rigidbody2D.AddForce(jumpForce);
    }
    }
    }
    
asked by anonymous 12.09.2015 / 19:46

2 answers

2

I can not see the error image but one thing that is wrong and I can point at seeing the code is how to access the Rigidbody2D component. Unity 5 has adjusted its syntax to better align with the C # (Object Oriented) Scope and Visibility concepts so where before you would have accessed the components of your GameObject as Attributes you now need to access with the GetComponent () or GetComponents () . You can find more information on how to properly use component access in the documentation ( videos and referrals )

In short, if you have Rigidbody2D attached to the same object as your script you can simply replace the Rigidbody2D with GetComponent<RigidBody2D>() call. 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 save a reference in your script instead of performing the search every time you need to use it.

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

    
14.09.2015 / 13:20
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 you to do it this way.

First create a Rigidbody2D object, then in Start * I get the reference from it that already exists in the created object, and inside the 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);
       }
    }
}
    
16.11.2015 / 13:40