- Home /
Duplicate Question
Reading data from one script to another
I have one object with 2 script (Data.cs and Math.cs) In first one (Data.cs) are information like mass ,size , shape, etc. In second one (Math.cs) are information like maxspeed, inertia, etc.
And I want read in second script (Math.cs) information from first script (Data.cs), for example if i wanna calculate inertia, i need mass and size data of my object, which are in first script (Data.cs)
My question is: How can i read this Data in Math script ?
Since they are on the same game object, you want to use GetComponent():
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
http://docs-jp.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Components.html
In my opinion, each game object should only have ONE main class, that is a mono behavior. I normally label them per game object. After that, I create static or public classes with no extension, or use interfaces. Basically, remove the need for a TON of Updates(), OnGUI()s, and all that other crap that you can consolidate to one file per object.
ok...and what if i want to set max camera distance which depends on size of the rendering object ? I have camera orbit script in main camera and data about size in object example: size = 5; distance = 5* size;
Follow this Question
Related Questions
Why does saving a script in runtime lose in game data? 1 Answer
Read data from Websites... 1 Answer
Is there a "Lightweight Monobehaviour"? 1 Answer
Reading XML Data C# 2 Answers
Copy script data to another class when refactoring? 0 Answers