My code is not working

My code is not working. On line 26, my rb.AddRelativeForce(Vector3.up) is not working. I am on lecture 36 of Complete C# Unity Game Developer 3D

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        ProcessThrust();
        ProcessRotation();
    }


    void ProcessThrust()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            rb.AddRelativeForce(Vector3.up);
        }
    }

    void ProcessRotation()
    {
        if (Input.GetKey(KeyCode.A))
        {
            Debug.Log("Rotating Left");
        }

        else if (Input.GetKey(KeyCode.D))
        {
            Debug.Log("Rotating Right");
        }
    }
}

Hi Hammad,

What does “is not working” mean? Are there any error messages in your console? And which line is line 26?

Hey hammad how are you doing .Would you please elaborate a little what kind of error you are facing the code looks fine to me .

This should have the component type declared:
myResults = GetComponent<ComponentType()

I am guessing it should be:
‘’’
rb = GetComponent?Ridgidbody?()
‘’’
Replace the ? with <>

yes it should be declaring a component you have confused the question you asked by attaching a code snippet please be clear
if you are asking for a rigid body it shoud be like
rb = GetComponent();

Hi Umais,

Here in the forum, the <> get interpreted as inline html or something like that. That’s why they disappear. We have to highlight the code and use the </> button to format it as code. Alternatively, we could use an escape character like \, which must get used like this \<Rigidbody\>. @edc237 used ? as a placeholder to visualise what Hammad chould check in his original code. He is right. It should be rb = GetComponent<Ridgidbody>() instead of rb = GetComponent().


@Hammad_Amjad, I edited your initial post, and formatted the code because it caused some confusion.

1 Like


here are my rocket components

It is fixed i decreased the mass then it worked

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms