Search Unity

Rigidbody2D error

Discussion in 'Scripting' started by MG, Jun 11, 2015.

  1. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    I have this code:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class mechanic : MonoBehaviour {
    5.     public float speed = 10.0F;
    6.  
    7.     void  Start ()
    8.     {
    9.  
    10.     }
    11.  
    12.     void Update() {
    13.         Vector2 dir = Vector2.zero;
    14.         dir.x = Input.acceleration.x;
    15.  
    16.  
    17.         if (dir.sqrMagnitude > 1)
    18.             dir.Normalize();
    19.      
    20.         dir *= Time.deltaTime;
    21.         Rigidbody2D.AddForce(Vector2 (dir * speed, 0));
    22.     }
    23.  
    24.  
    25.  
    26. }
    27.  
    And im getting this error

    Code (CSharp):
    1. Assets/mechanic.cs(21,29): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.AddForce(UnityEngine.Vector2, UnityEngine.ForceMode2D)'
    2.  

    Why?
     
    Last edited: Jun 11, 2015
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You need an object reference to the component. Get one using GetComponent.
     
  3. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    Could you give me an ex. please?
     
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    There are plenty in the documentation for GetComponent. You would use GetComponent<Rigidbody2D>() instead of just Rigidbody2D
     
  5. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    Code (CSharp):
    1. Changed:
    2.  
    3. GetComponent<Rigidbody2D>().AddForce(Vector2 (dir * speed, 0));
    Error:


    Code (CSharp):
    1. Assets/scripts/mechanic.cs(21,54): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected
    2.  
    3.  
    4. Assets/scripts/mechanic.cs(21,45): error CS1502: The best overloaded method match for `UnityEngine.Rigidbody2D.AddForce(UnityEngine.Vector2)' has some invalid arguments
    5.  
    6. Assets/scripts/mechanic.cs(21,45): error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Vector2'
    7.  
    8.  
     
  6. ANTARES_XXI

    ANTARES_XXI

    Joined:
    Dec 23, 2014
    Posts:
    141
    If you use Unity4 you can just write rigidbody2D with lowercase 'r' and not uppercase 'R'. For Unity5 you should use GetComponent<Rigidbody2D>()
     
  7. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    Antares_xx i am using Unity5,

    and geting the errors as i wrote before
     
  8. ANTARES_XXI

    ANTARES_XXI

    Joined:
    Dec 23, 2014
    Posts:
    141
    use
    Code (CSharp):
    1. new Vector2
    instead of
    Code (CSharp):
    1. Vector2
     
  9. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    This is going great,

    See picture for the errors
     

    Attached Files:

  10. ANTARES_XXI

    ANTARES_XXI

    Joined:
    Dec 23, 2014
    Posts:
    141
    Provide your latest code here
     
  11. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class mechanic : MonoBehaviour {
    5.     public float speed = 10.0F;
    6.    
    7.     void  Start ()
    8.     {
    9.        
    10.     }
    11.    
    12.     void Update() {
    13.         Vector2 dir = Vector2.zero;
    14.         dir.x = Input.acceleration.x;
    15.        
    16.        
    17.         if (dir.sqrMagnitude > 1)
    18.             dir.Normalize();
    19.        
    20.         dir *= Time.deltaTime;
    21.         GetComponent<Rigidbody2D>().AddForce(new Vector2 (dir * speed, 0));
    22.     }
    23.    
    24.    
    25.    
    26. }
     
  12. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    You're trying to create a new Vector2 using an entire vector as the X component.

    I think what you're looking for is:

    Code (CSharp):
    1. GetComponent<Rigidbody2D>().AddForce(new Vector2 (dir.x * speed, 0));
     
  13. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    Thanks Limeoats.

    That makes the errores disappear.

    But now is new problem starting to appear.

    The script is attached to a ball with rigidbody2d.
    And when i move arround with the ball (By moving the screen) the ball start to go though 2D colliders. Its like its squeezses though the objects.

    How can i improve the mechanics?
     
  14. Limeoats

    Limeoats

    Joined:
    Aug 6, 2014
    Posts:
    104
    It depends on what you're going for. You can add more physics properties to the ball to slow it down when it goes through, or tighten the space so it can't fit through if that is what you're going for. It all depends on what you're trying to do.