I'm really, really new to games - And I'm having problems with the Roll a Ball tutorial

Hi everyone, I’m a Product Designer that had an idea for a mobile game pop up, and I’m trying to figure out Unity on my own.
I know nothing about C# or coding in general, although I’m fast to learn the logics of a program, so I was trying to use the tutorials to have some experience with Unity before going for the game. I got a bit lost on the scripting par of the tutorial, but unterstood it well enough. The problem is that the code they offer here: Environment and Player - Unity Learn

Causes a problem that I can’t solve, popping up on the program like this:

Assets/Scripts/PlayerControler.cs(14,14): error CS0305: Using the generic method UnityEngine.Component.GetComponent<T>()' requires 1’ type argument(s)

I can see the tutorial was made on an older version of Unity, and that might be affecting the outcome. Anyone knows how to solve this on Unity 2018.1, or knows of a better player controler script for this version?

Copying/pasting the error is good. The other thing thing you should do is copy/paste the actual code. If the code is long, you can paste just the line where the error occurs, which you can easily find by double-clicking the error.

But basically you need to figure out what type of component this line of code is trying to get, and specify that between < and >. It should look something like this:

foo = GetComponent<SomeTypeGoesHere>();

where SomeTypeGoesHere is the name of a component type (e.g. MonoBehaviour subclass, or Rigidbody, or something like that).

2 Likes

Thank you. So, another problem is that I can’t seem to find the line that appears on the error. This is the code I used, the same that is available on the tutorial page:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public float speed;

    private Rigidbody rb;

    void Start ()
    {
        rb = GetComponent();
    }

    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

        rb.AddForce (movement * speed);
    }
}

You need to specify in the GetComponent() which component you are looking for.

In that case that would look like:

rb = GetComponent<Rigidbody>();
3 Likes

Yep. You can figure that out because you’re assigning the result to rb, and the type of rb (as declared on line 8) is Rigidbody.

As for finding the line with the error: literally just double-click the error in the Console. It should open your code editor and put the cursor on the offending line. Or you can read it yourself — it says “PlayerControler.cs(14,14)”, so that’s line 14 (and column 14) of PlayerControler.cs. (Which doesn’t actually exist, so I now believe you retyped the error manually instead of copy/pasting it… but close enough! :slight_smile: )

1 Like

Thanks, I hadn’t seen the missing Rigidbody component. Now it works! Also, I did copy paste the error, but I might have missed a few lines when I copy pasted the code, hence it not matching the error line.
Thanks guys, you’re awesome! :slight_smile:

1 Like