- Home /
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!
would ideally be good to see what is in the script SetInitialOrbitalVelocity
Answer by mjc33 · Feb 17, 2020 at 08:53 PM
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;
Your answer
Follow this Question
Related Questions
How to make [HideInInspector] show in Debug Mode? 1 Answer
Multiple Cars not working 1 Answer
Unity can't use the script. 0 Answers
Distribute terrain in zones 3 Answers
pubblic variable don't get change from script with OnTriggerStay 1 Answer