Variable from a script attached to a specific game object

Hi, I’m new to both C# and Unity.

I have the following line of code that doesn’t show any errors, but is also not working:

orbitalVelocity = GameObject.Find("Earth").GetComponent<SetInitialOrbitalVelocity>().initialVelocity + orbitalVelocity

I am trying to obtain the ‘‘OrbitalVelocity’’ from the script attached to the game object “Earth” and add it to the current “OrbitalVelocity” of the object that my new script will be attached to.

Any help will be much appreciated!

First thing I’d say is it definitely “Earth” and not “earth” or something else? You are best using references to Gameobjects to avoid issues here.

Otherwise it is likely the issue lies in the SetInitialOrbitalVelocity component, can we see the code for that?

Most likely unrelated to it not working but you can use the += operator here, like

 orbitalVelocity += GameObject.Find("Earth").GetComponent<SetInitialOrbitalVelocity>().initialVelocity;