- Home /
rigidbody deprecated confusion issue C#.
Hi all, trying to add force to a textbox in C#, for whatever reason while I follow either a Unity official tutorial or one written by someone else, something is not right.
So, I have an object on my screen, its just a text box. I have a rigidbody3d object applied to it, Following the templates that I have seen in C#, this is supposed to work:
using UnityEngine;
using System.Collections;
public class WordMover : MonoBehaviour {
public float power = 100;
public Rigidbody rb3D;
// Use this for initialization
void Start () {
rb3D.GetComponent<Rigidbody>();
rb3D.AddForce(Vector3.up * power, ForceMode.Acceleration);
}
This is according to what I saw at: http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
This throws the following error:
UnassignedReferenceException: The variable rb3D of WordMover has not been assigned. You probably need to assign the rb3D variable of the WordMover script in the inspector.
I don't get it, the script is sitting on the object that I want moved, what am I missing here, what is it looking for?.
Then I looked at one of Unity's official video tutorials and it uses some sort of deprecated way of doing it:
I tried doing it this way and VisualStudio told me the following:
CS0619 C# 'Component.rigidbody' is obsolete: 'Property rigidbody has been deprecated. Use GetComponent() instead. (UnityUpgradable)'
Answer by BackslashOllie · Dec 14, 2015 at 10:00 AM
Its just a syntax error.
Change:
rb3D.GetComponent<Rigidbody>();
To:
rb3D = GetComponent<Rigidbody>();