…
So can I get an example of the usage of GetComponent()?
var player= GameObject.FindWithTag(“Player”);
var rb = player.GetComponent();
rb.velocity = blabla
I don’t get it. Line #3 has no semicolon and I (maybe) think that means there is more code (?). Could you please give me a short working script that MonoDevelop will recognize?
“Assets/PlayerControls.js(16,59): BCE0043: Unexpected token: ).” is the exact error I get when I try to run this code.
Monobehaviour used to include a reference for a rigidbody2d. It doesn’t anymore. Use GetComponent to find the Rigidbody2d now.
The above psuedo code is that example, it’s pretty straightforward.
I’m just learning Unity, so pseudo code is a bad idea. Here’s my exact code. Could you please complete this code?
#pragma strict
var moveUp : KeyCode;
var moveDown : KeyCode;
var speed : float = 10;
function Update ()
{
if (Input.GetKey(moveUp))
{
//Please tell me what goes here to make my paddle move up.
}
else if (Input.GetKey(moveDown))
{
//Then tell me what goes here to make my paddle move down.
}
else
{
//Finally could you tell me what goes here to make my paddle not move at all.
}
}
/ The tutorial that I’m trying to follow uses MonoDevelop code that is old so my compiler yelled at me when I used it.
I got it!
gameObject.GetComponent(Rigidbody2D).velocity.y = + speed * Time.deltaTime; isn’t obsolete.
Looks like my internet connection answered my question.
If you’re learning, use C# its easier to understand and you can achieve more with it than UnityScript.
The usage is also different, in C# its .GetComponent();
in UnityScript its
.GetComponent.();
C# is easier? Then why does UnityScript even exist?
By the way, aren’t "GetComponent();" and “GetComponent.();” the same?
They do the same thing, but they are a different syntax. Because they are 2 different languages.
Oh… O.K.
What language do you prefer using with Unity? And how do I compile with Unity for this language?
I prefer C#, you just need to click create new c# script and attach it to the game object. For example an empty C# script would look like this:
move_player.cs
using UnityEngine;
public class move_player: MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Note the name of the MonoBehaviour class must be the same as the script’s filename.
Now assuming we attach this script to the same gameobject as your Rigidbody2D we can store the Rigidbody2D in an object and access it like this:
using UnityEngine;
public class move_player : MonoBehaviour {
private Rigidbody2D rb2d; //This will store our Rigidbody2D Component
// Use this for initialization
void Start () {
rb2d = gameObject.GetComponent<Rigidbody2D>(); //When this object is loaded get the component Rigidbody2D in this gameobject and store it in rb2d
}
// Update is called once per frame
void Update () {
if (Input.anyKeyDown) // Waits for any key to be pressed
{
rb2d.AddForce(Vector2.right); //applies a force pushing right on your Rigidbody2D
}
}
}
It doesn’t. Unity has announced that its deprecated. Don’t put any effort into learning it, its going away.
I can’t help but notice the big letters saying ‘Unity 4.3’. The 5.x series introduced some major changes to how scripting is done. For learning purposes I would highly recommend using at least version 5.0. Otherwise you are going to be back here quite frequently with similar questions.
Many thanks for this example Nkoeppel. You helped me get past a sample script in the Ruby’s Adventure 2D tutorial that was not compiling when initialized as the tutorial proposes:
NOT WORKING:
public class RubyController : MonoBehaviour
{
Rigidbody2 rigidbody2d;
void Start()
{
rigidbody2d = GetComponent();
} …
NOW WORKING
public class RubyController : MonoBehaviour
{
private Rigidbody2D rb2d;
void Start () {
rb2d = gameObject.GetComponent();
}…
Thanks again, and on to the next challenge!