- Home /
Variables in base class not changing, sorta...??
I have a base class like this (in Standard Assets/Scripts):
using System; using UnityEngine;
public class DataSource : Monobehavior { public DateTimeOffset lastUpdated = DateTimeOffset.Now; }
and a subclass or two that gets data and sets this, like so...
public class DataGatherA : DataSource
{
void Update()
{
.. get data ..
lastUpdated = DateTimeOffset.Now;
}
}
Then, in a GUI script, I access this var. Here's the problem - it never seems to change at least on screen:
var dataSrc : DataSource = GameObject.Find("MyDataSource").GetComponent(DataSource);
function OnGUI()
{
wr = GUILayou.Window (1, wr, DisplayData, "As of "+dataSrc.ToString("h:mm:ss"));
}
Note that it does display the first (constructed) time ok, so I know it's getting that. And I know the Update on the subclass is called because I have other things going on that depend on it, work fine. It's just this dang window title that's not updating. I tried putting DateTimeOffset.Now.ToString() into the window title, that works. But it's not pulling the current value from the base class.
What am I doing wrong?
$$anonymous$$aybe it has something to do with mixing javascript and C# code?
Works for me exactly as you wrote it. ($$anonymous$$inus the syntax errors) Probably you are doing something else in the part of the code you didn't post.
Thanks for trying! The 'Gather'class just finds some game objects and sets data on them using random numbers (the idea is to replace this with an actual data gatherer device later). Not much else. Ok, so I'll start commenting out the code I didn't include here and see if/when it starts working. Good to know it can work though! I've been going nuts on this.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122342im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why the variable updates only twice? 1 Answer
How to change the rate of decrease in a variable in the update function 1 Answer
Access a variable from another script in update function 1 Answer
Why a public varibale is not accsible inside Update() ? 1 Answer
coroutines : trouble editing and accessing the same public variable 4 Answers