Unity中公共变量设为静态后无法在Inspector编辑的问题
Hey there! Let's work through this common Unity beginner issue—you're closer than you think, and the fix is simpler than making days static. The core problem here is a misunderstanding of variable scoping: you don't need days to be static for your private hours variable to access it, even within the same script.
days Editable in the Inspector + Auto-Compute hours Here are three solid solutions depending on your needs:
1. One-Time Initialization (For Fixed Values)
If you only need to calculate hours once when the game starts (and days won't change during runtime), just use Awake() or Start() to compute it:
public class TimeCalculator : MonoBehaviour { // Visible and editable in the Inspector public int days = 3; // Internal value for code use private int hours; void Awake() { hours = days * 24; } // Example usage in your code void LogTotalHours() { Debug.Log($"Total hours: {hours}"); } }
This keeps days as a regular public instance variable (so Inspector-friendly) and lets hours access it directly without static.
2. Real-Time Sync with Properties (For Dynamic Values)
If days might change during runtime (via code or Inspector tweaks), use a C# property to auto-update hours whenever days is modified. To keep the Inspector visibility, we'll use [SerializeField] on a private backing field:
public class TimeCalculator : MonoBehaviour { // Hidden from other scripts, but visible in Inspector [SerializeField] private int _days = 3; // Internal computed value private int hours; // Public property to control access to _days public int Days { get => _days; set { _days = value; // Auto-update hours every time Days is set hours = _days * 24; // Optional: Add validation (e.g., prevent negative days) _days = Mathf.Max(_days, 0); } } }
Now, whether you adjust _days in the Inspector or set Days via code, hours will always stay in sync.
3. Edit-Mode Real-Time Updates (For Editor Workflow)
If you want hours to update while you're working in the Unity Editor (without entering Play Mode), use the OnValidate() method—it triggers automatically whenever you modify a variable in the Inspector:
public class TimeCalculator : MonoBehaviour { public int days = 3; private int hours; void OnValidate() { // Update hours immediately when days is changed in the Inspector hours = days * 24; // Add validation to avoid invalid values days = Mathf.Clamp(days, 0, int.MaxValue); } void Start() { // Double-check the value when entering Play Mode hours = days * 24; } }
Key Takeaways
- Never use static here—static variables are for shared values across all instances of a script, which isn't what you need. Regular instance variables work perfectly for accessing
daysfromhoursin the same script. - Use
[SerializeField] private int daysif you want to hidedaysfrom other scripts but still let the Inspector edit it (this is a best practice to enforce encapsulation). - Pick the solution that matches your workflow: one-time setup, dynamic runtime changes, or editor-side real-time updates.
内容的提问来源于stack exchange,提问作者Aaa

