Hello! I’m currently enrolled in an online class for game design in Unity, and we’re instructed to copy a bit of code to a test scene that lets a bullet-like object be shot out of a cube. The script is to be attached to the bullet, and that should be all. However, the script (despite being copied from the lesson exactly is returning the error shown below.
rb = GetComponent();
rb.velocity = transform.forward * speed;
Can someone please explain to me what I’m doing wrong or if theres a better alternative? Thanks in advance,
The problem is that you are not specifying which component you actually want to get from the GameObject. There is no way for the GetComponent function to actually know what type of component you want to get since there are several of them. (Rigidbody, Collider, etc)
In your case it looks like you’re trying to get a component of type Rigidbody, so you actually have to specify that when calling the function like this:
rb = GetComponent<Rigidbody>();
Remember that Unity has a very well documented website here, that can be used to check how different functions work or how classes work. Here you can find the documentation for the GetComponent function.