I want my script to publish some variables at runtime that could be read by other scripts, but they cannot modify them. Also I don't want these variables to appear at Unity's editor.
Thank you!
I want my script to publish some variables at runtime that could be read by other scripts, but they cannot modify them. Also I don't want these variables to appear at Unity's editor.
Thank you!
best way is to either use functions or properties
functions will work in js and c#:
(js)
var test : float = 1;
function getTest() : float
{
return test;
}
(c#)
float test = 1;
public float getTest()
{
return test;
}
the alternative is properties in c#:
float test = 1;
public float Test { get { return test; } }
You didn't say what language, so I'll assume C#. There may be other ways, but the only one I can think of - is to use a Property, and define a get function, but not a set function.
I've just discovered that you can actually access the private variables in one script from other script! You can use them exactly as if they were public variables (at least in Javascript, didn't tested in C#).
I guessed that the 'private' keyword would keep the variable isolated from other scripts, but it's more like a "non-publish-in-editor" tag.
Edit: You can access the private variables only when accessing the script this way:
var myScript = Vehicle.GetComponent(myScriptName);
var value = myScript.privateValue;