I am newish to unity and I have a little problem where a script I have written that is attached to the character declares a variable and sets it equal to a number. If I go into the script and change the number and rebuild the script then go back into unity the number is still set to the original number. I can of course click on the inspector box and change it from within unity, but I can see how this can be problematic later on when doing large projects if my script says one thing and I expect it to be that then my game object says another. Why isn’t the inspector/unity updating the number to whatever I put in my script?
It would break Unity if it worked like that. The point of using public variables is that they get their value from the inspector…imagine spending time tweaking your variables until you get just the right behavior, only to make an unrelated change in your script and poof! your variables all all reset. That would be bad stuff, no? Unity works the way it does for a reason… If you want your variables to only take their value from the script, then don’t use public variables. And if you do use public variables, then you should expect that the value is not necessarily what you have in your script.
–Eric
Okay I found out how to get the inspector/game to use my coded values. I pressed the cog wheel on the script in the inspector and went to “Reset” and the values changed to those in my script. I also tried Assets->Refresh, but that didn’t work. Is this common to have to do this? Or is there a way to set some options so that the changes will immediately be made when I re-compile a script? I could just see myself forgetting to reset the script in the inspector at some point and getting bugs so I would prefer it to update on re-compile.
Okay yea that makes sense. I know what public and private is from a programming standpoint, but didn’t think about it from an inspector standpoint. I just like my code to reflect what is actually going to happen in the game. If my code says x force should be 100, but the inspector has it at 300 that inconsistency is a problem. I can now see why they did it this way though and work with it. The game using private variable values as the code is defined is exactly what I was looking for and now know that I’ll only be using public variables for things that I might have to make a lot of tweaks on to get right so I don’t have to change/recompile over and over.
Not just for that, but also if you have different values on different objects. e.g., maybe a speed variable where different enemies have different speeds.
–Eric