- Home /
Changing variable values not having any effect
class RainManager extends MonoBehaviour
{
static var instance : RainManager;
@HideInInspector var groundPos : float = -1.5;
@HideInInspector var particleNum : int = 250;
@HideInInspector var areaSize : float = 13.0;
@HideInInspector var areaHeight : float = 5.0;
@HideInInspector var fallingSpeed : float= 22.0;
@HideInInspector var particleSize : float = 0.4;
@HideInInspector var flakeRandom : float = 0.1;
public function Awake ()
{
instance = this;
// Always prints the same thing, regardless of the above values...
Debug.Log( particleSize + " // " + particleNum );
}
}
The above variables were originally public, so they showed in the inspector, but I later changed them to @HideInInspector. But now modifying the values in the script doesn't have any effect. I think the previous inspector values are overwriting the script values? How can I reset the inspector so it's not overwriting the data? I've already tried a few things several times…
Clicking the cog and selecting "Reset"
Changing the vars to private, letting the script compile, and then switching back to @HideInInspector
Removing the vars, recompiling, and replacing the vars
Deleting the gameObject and creating a new one and attaching the new script
Whenever I do these things, the values are set back to whatever is in the script, but as soon as I change the values they refuse to make any further changes.
Answer by Eric5h5 · Nov 04, 2013 at 01:00 AM
Public variables always get their values from the inspector. Unity would break horribly if it ever worked any other way. If you only want variables to get their values from the script, don't make them public.
is declaring a default var, e.g. — var myVariable = 0.0
— does that make it public? How do I make it private but still accessible from other scripts?
The default for Unityscript is public, so "`var myVariable`" is public. Typically you use properties (getters/setters) for variables that are accessed from other classes.
Oh… I was just following the advice of some dude named Eric5h5: http://answers.unity3d.com/questions/21043/stupid-question-about-public-private-variables.html
That was from 2010...Unityscript didn't have properties back then (or if it did, it was undocumented). Although $$anonymous$$ike 3's answer on that page amounts to more or less the same thing, even if it's not quite as convenient.
Wow, I haven't even heard about this before. Everything I've ever read said to use @HideInInspector. I'm trying to google information about how to get / set variables in other scripts, and as far as I understand, it works essentially like accessing a static variable, but for private variables? Unfortunately it seems heavily un-documented. Almost everything I can find results with an, "It's not working". Can you give me a basic example Eric?
Your answer
Follow this Question
Related Questions
When to use private var. and public var.? 4 Answers
C# - public variable not exposed to the Project Panel? (n00b to c#) 1 Answer
[C#] Assets/BreakObject.cs(20,62): error CS0165: Use of unassigned local variable `broken' 1 Answer
When to use public or private variables for the inspector? 2 Answers
Share Public Classes 1 Answer