Difference between Scripts

In the course, we were asked to add a Rigidbody using code. They showed it like this;

image

However, I did it like this;

    void Start()
    {
        scoreboard = FindObjectOfType<scoreboard>();
        gameObject.AddComponent<Rigidbody>().useGravity = false;
    }

As far as I can tell, the enemy is doing exactly what it was doing on Rick’s screen. What is the difference?

This refers to the Complete C# Unity Game Developer 3D course on Udemy (Argon Assault).

Hi,

There is no difference. Rick assigns the returned Rigidbody object to a variable to increase the readability of his code. Of course, he could do everything in one line like you but he felt that his code could become less readable if he did that.


See also:

1 Like

Cool, thank you!

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

Privacy & Terms