How can I use a float Variabel in an other Sript?
Hello,
There are many answers about that, but I do not understand there for my work.
I use the LocationService Script from https://docs.unity3d.com/ScriptReference/LocationService.Start.html. And it works well. The latitude and longitude I would like to use in a javacript and do some calculation with it. How is the correct Way to put the latitude and longitude to the Calculation-Script?
Thanks for answering!
Answer by Tasarran · May 04, 2017 at 10:36 AM
First of all, the variables you want to refer to have to be marked public in the original script.
Secondly, you have to get the GameObject that the original script is attached to, and you can get the variables this way:
gameObjectScriptIsOn.GetComponent<NameOfOriginalScript>().nameOfVariable
Tasarran
Is it then: CalculationScript.GetComponent().latitude ?
This is not working. There are some Tokenmistakes.
@$$anonymous$$ First of all, don't add a comment as an answer, comment on my answer ins$$anonymous$$d, please.
Secondly, you didn't do what I said. You left some stuff out, and made a couple of mistakes.
Let me try again, I'm going to add a little bit this time, too.
I'll dissect the line of code bit by bit, but here is what is needs to look like, assu$$anonymous$$g the following: Name of the original script: "LocationService" Name of the variable you want to assign LocationService.latitude to: "lat" You'll also need to know what the name of the GameObject is that the LocationService is attached to; I'm going to assume it is called "LocationObject"
In your new script, you need this line:
lat = GameObject.Find("LocationObject").GetComponent<LocationService>().latitude;
What does this do?
lat =
This is the variable in the new script that we want to hold the variable from LocationService
GameObject.Find("LocationObject")
Think of this as if we are selecting the GameObject in the Heirarchy. We have to start at the top.
GetComponent<LocationService>()
Now we step down to the Component level. We have to put the 'LocationService' because there can be more than one Component, and we have to tell Unity which one we want, even if there is only one currently.
.latitude
And then at the end, we grab the actual variable 'latitude' from the script.
When I convert the Javasript in c# it still works get. Thank you for your great answer.
Your answer
Follow this Question
Related Questions
Can I use ZeroC Ice with Unity? 0 Answers
Moving slowly copy of object to random position on x,z? 0 Answers
Trying to get animation to work. Problem with JS. 1 Answer
How to convert float to long 4 Answers
Can't build my Project 1 Answer