Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

New to Unity new to C# - CS0619

Discussion in 'Editor & General Support' started by SNWZY3, Mar 4, 2015.

  1. SNWZY3

    SNWZY3

    Joined:
    Mar 1, 2015
    Posts:
    5
    I have pretty much just started using unity as v4 was phased out. I did a number of tutorials in the "Learn section" I was halfway through the "Space Shooter" tutorial when I upgraded. I decided I would just scrap the v4 project and start afresh on v5 (as all my gameobjects within the hierarchy where removed. Not a big deal)

    I was trying to copy the sample code that the instructor was typing only to be given the following error...

    "Assets/Scripts/PlayerController.cs(20,17): error CS0619: `UnityEngine.Component.rigidbody' is obsolete: `Property rigidbody has been deprecated. Use GetComponent<Rigidbody>() instead. (UnityUpgradable)'"

    I've done some Googling around (past hour or two) to no avail... Anyone able to help me with this?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Boundary
    5. {
    6.     public float xMin, xMax, zMin, zMax;
    7. }
    8.  
    9. public class PlayerController : MonoBehaviour
    10. {
    11.     public float speed;
    12.     public Boundary boundary;
    13.  
    14.     void FixedUpdate ()
    15.     {
    16.         float moveHorizontal = Input.GetAxis ("Horizontal");
    17.         float moveVertical = Input.GetAxis ("Vertical");
    18.  
    19.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    20.         rigidbody.velocity = movement * speed;
    21.  
    22.         rigidbody.position = new Vector3
    23.             (
    24.                 Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
    25.                 0.0f,
    26.                 Mathf.Clamp (rigidbody.position.z,   boundary.zMin, boundary.zMax)
    27.             );
    28.     }
    29. }
    Posted code if anyone is willing to proofread and tell me where I'm going wrong.

    Also when I finish writing the code I get a dialogue box telling me if I would like to exchange the variables with "GetComponent<Rigidbody>()" not quite sure what this does, does it refer to an older version of the UnityEngine where it can find it? Even when I use that it throws compiler errors at me.

    Please help!! :)

    As a note, I'm really well into learning how to develop games and want to expand my knowledge by doing these tutorials. If they have all now been thrown out because of these changes, where am I best going to learn further? Thanks.
     
  2. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    Yes, if you replace all instances of the the word "rigidbody" in that script for "this.GetComponent<Rigidbody>()" it get rid of the warning message.

    However it would be even better to cache the rigidbody component.

    For instance, declare a new property:

    Code (CSharp):
    1. private Rigidbody myRigidbody;
    2.  
    3. private void Start()
    4. {
    5.     this.myRigidbody = this.GetComponent<Rigidbody>();
    6. }
    And in the script use "this.myRigidbody" instead of "rigidbody".
     
    muhyunanadil likes this.
  3. SNWZY3

    SNWZY3

    Joined:
    Mar 1, 2015
    Posts:
    5
    Sorry for the late reply.

    It worked!! I moved the sample script you provided and put it within my "PlayerController" class.

    Would this work outside of the class so it can be used within the entirety of the script or would this have to be added into every class that is using the "rigidbody" component?

    Thanks for the help!
     
  4. CatsCranium

    CatsCranium

    Joined:
    Mar 2, 2015
    Posts:
    10
    Since 'Manny Calavera's "myRigidbody" variable is declared as a private, it can only be accessed from within the class. Declare it as a public variable if you want it to be used outside of the class (ie: "public Rigidbody myRigidbody"). Or even better, declare it as an auto-property with a public getter and a private setter, so that only the class can change it but anyone/any class can access it:

    Code (CSharp):
    1. public Rigidbody myRigidbody { get; private set; }
    2.  
    3. private void Start()
    4. {
    5.     this.myRigidbody = this.GetComponent<Rigidbody>();
    6. }
     
  5. SNWZY3

    SNWZY3

    Joined:
    Mar 1, 2015
    Posts:
    5
    Appreciate the help! Thank you.

    After using Manny Calavera's script I found another.

    Code (CSharp):
    1.     private Rigidbody rb;
    2.  
    3.     void Start ()
    4.     {
    5.         //Declare the rigidbody within the script
    6.         rb = GetComponent<Rigidbody>();
    7.  
    8.         rb.angularVelocity = Random.insideUnitSphere *
    9.     }
    This just makes it that little bit smaller and the less complicated the better as far as I'm concerned. I'm guessing the code you posted would also help if I added the "public getter"?
     
  6. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    Since you are beginning and seem interested in putting the time necessary to learn I'll give you some hints.

    Learn better coding practices from the start. For instance, you don't gain anything in abbreviating your code. In fact, you just make it more confusing by omitting 'this.' or shortening property names.

    For instance, when I see 'rb' in the code above I don't know what it is or what's the scope unless I read the entire code. 'rb' could be Rigidbody, RenderBuffer or whatever.. Also, this.rb would at least tell me that this is a member of the class rather than some local variable. It's important to have a consistent coding style throughout all your code.

    Also, I would never expose the rigibody of an object as public. If other objects need to mess with this object then they should send a message, for instance, by calling a method that you declare in the class.

    Anyway, keep it up and good luck.
     
  7. SNWZY3

    SNWZY3

    Joined:
    Mar 1, 2015
    Posts:
    5
    Hi Manny,

    Thanks for that, makes me realise just how many different variables I have to play with...

    Based on what you said I think it would be good practice to annotate all my scripts with comments before actually signing off on them entirely just so I can understand myself what I've written. Especially seeing as I'm in the infancy stage of coding.

    Anyway, thanks for all the help guys. It's very much appreciated.
     
  8. cacbic

    cacbic

    Joined:
    Mar 10, 2015
    Posts:
    1
    SNWZY3
    Do you mind pasting your final code, having the same problem trying to follow the first roller ball tutorial which probably has not been updated to Unity 5.
     
  9. SNWZY3

    SNWZY3

    Joined:
    Mar 1, 2015
    Posts:
    5
    Hi cacbic,

    No problems. Although just copying the code wouldn't be beneficial to you in the long run.

    Might be better for you to use the example the manny provided above. You just need to create something the code can refer to. Then when trying to use the rigidbody component just use the same script reference. I'm at work currently so unable to post the code.

    Let me know if you had any success.
     
  10. CatsCranium

    CatsCranium

    Joined:
    Mar 2, 2015
    Posts:
    10
    Here is a script that when used instead of MonoBehaviour auto-caches the standard components:
    https://github.com/nickgravelyn/UnityToolbag/blob/master/CacheBehaviour/CacheBehaviour.cs

    Example usage:
    Code (CSharp):
    1. public class ExampleClass : UnityToolbag.CacheBehaviour
    2. {
    3.     void Start()
    4.     {
    5.         rigidbody.AddForce(-50f,0f,0f);
    6.     }
    7. }
    I'm not sure I recommend using it though, might be better to learn how to cache efficiently yourself.
     
  11. InYourEyes

    InYourEyes

    Joined:
    Feb 9, 2016
    Posts:
    3
    hey guys i have a problem with mouselook.cs Assets/Standard Assets/Character Controllers/Sources/Scripts/MouseLook.cs(60,21): error CS0619: `UnityEngine.Component.rigidbody' is obsolete: `Property rigidbody has been deprecated. Use GetComponent<Rigidbody>() instead. (UnityUpgradable)' please help here is the codes
     

    Attached Files:

  12. mikesgameworld

    mikesgameworld

    Joined:
    Jun 10, 2016
    Posts:
    2
    :( I am in a 2d Mario tutorial and I am stuck on an error saying:
    "NotSupportedException: rigidbody property has been deprecated
    UnityEngine.GameObject.get_rigidbody () (at C:/buildslave/unity/build/Runtime/Export/UnityEngineGameObject_Deprecated.cs:23)
    (wrapper dynamic-method) UnityEngine.GameObject.GameObject$get_rigidbody$ (object,object[]) <IL 0x00006, 0x0007e>
    Boo.Lang.Runtime.RuntimeServices.GetProperty (object,string) <IL 0x00043, 0x001c2>
    UnityScript.Lang.UnityRuntimeServices.GetProperty (object,string) <IL 0x00017, 0x00111>
    playerProperties.Update () (at Assets/2D Mario Assets/Scripts/playerProperties.js:50)".
    I think I am on part 4 - 3 player properties playerStates.

    Here is my script:
    Code (CSharp):
    1. function Update ()
    2. {
    3.     var playerC
    4. ontrols = GetComponent ( "playerControls" );
    5.     if ( changeMario )
    6.     {
    7.         SetPlayerState ();
    8.     }
    9.     if ( canShoot )
    10.  
    11.     {
    12.         var clone;
    13.         if ( Input.GetButtonDown ( "Fire1" ) && projectileFire && playerControls.moveDirection == 0 )
    14.         {
    15.             clone = Instantiate ( projectileFire, projectileSocketLeft.transform.position, transform.rotation );
    16.             clone.rigidbody.AddForce ( -90, 0, 0 );
    17.         }
    18.         if ( Input.GetButtonDown ( "Fire1" ) && projectileFire && playerControls.moveDirection == 1 )
    19.         {
    20.             clone = Instantiate ( projectileFire, projectileSocketLeft.transform.position, transform.rotation );
    21.             clone.rigidbody.AddForce ( -90, 0, 0 );
    22.         }
    23.     }
    24.     else
    25.         return;
    Can anyone help me? Thanks!:)
     

    Attached Files: