Debug.Log not updating value?

So I made a script referring to another script’s value, but when I debug it to check it if works, it displays the same value over and over, even though that variable’s value is changing.

Here is the variable I’m referring to (from another script):

public float timeline = 7f;

And here is the debug line inside the Update method (of my script):

void Update () {
    Debug.Log ("Time is: " + timeline);
}

Not only have I changed the value to “12” in the Inspector, but the value is also constantly increasing at runtime. Yet when I hit Play, it just keeps displaying “7” in the console output instead of “12”, and the value doesn’t increase either. It just keeps outputting “7”. I tried removing the “= 7f” and just letting it take the information from the inspector alone, but that didn’t work.

Anyone any ideas what might be the cause? I already tried searching the scripts this is getting referenced from, incase there was somehow another declaration of this variable, or it was overridden somewhere, but it’s not.

Did you reference the variable, like this ?

timeline = GetComponent<Script>().timelineValue;

Then try to print it out ?

1 Like

could be multiple instances in the scene and you’re just looking at one of them

Add context to that debug.log. so that when you click on the log in the console panel it’ll highlight the object in question that called the Log

Debug.Log ("Time is: " + timeline, this);
1 Like

Turns out it’s an issue directly related to how the other script works. It’s a part of a large system of scripts (it’s a part of an asset I downloaded), therefore the issue is very specific to that asset, rather than to general coding.

I will contact the asset’s developer. Thanks for your help anyway.

Regarding the snippets from your first posts, it appears to be an issue with the way you’re trying to “reference” it.
Value types cannot be directly referenced, you need a reference to the actual script instance that exposes this field. Just what @methos5k already suggested in his post.
Cache the reference to the script instance and then log the field to the console. It should print the actual value.

1 Like

That’s what I already have. I made an empty field where I put in the GameObject containing the script I need.

The problem is, that I need to derive my script from that other script (rather than MonoBehaviour), otherwise it won’t let me reference the variable. Therefore all its options are duplicated into my script’s Inspector window (including “timeline”), which is why it’s staying at “7” (because it’s now referencing the duplicated value which isn’t being used in any calculations).

Oh, okay… then there is some more information than before. If your script is derived from another, you shouldn’t need to duplicate the variables. You can access them in the derived class, already. Then if that’s the case, you need only have 1 script (that’ll have all of the values) instead of 2.
If you want to give a short description of these scripts, perhaps you could get further comments/insight on this, if you’d like :slight_smile:

1 Like

All the variables are duplicating themselves (that’s what I’m contacting the developer about), and I don’t know how to stop it from happening.

The script I’m deriving from is a script which is also derived by a third script. The script I’m referencing is just pure calculations, but the third script is where all the Inspector editing happens, so I can’t only have 1 script, since the script being derived from, is crucial.

Essentially the problem is, that I’m trying to only access a single variable, but the whole script is being duplicated into my one, and I don’t know how to prevent it from doing so.

When you create a subclass, it brings all of the parent class variables with it. It’s unlikely that the author will respond with a way to fix that.
I didn’t completely follow your explanation, but I imagine that you can either derive and have that one script (where you can edit the variables from the parent class in the inspector, also).
Or have a separate script that accesses 1 or more variables and does whatever calculations you need in that, without that script also deriving from the other(s).

1 Like

The first suggestion wouldn’t work, since the 3rd script already handles all the Inspector editing, and having two scripts doing the same thing would be pointless, not to mention it would clutter everything up.

From what I understand in your second suggestion, I could make a new script, and reference the one I already have (which is referencing the parent class), as a sort of channel script. The problem with that, would be that there aren’t any calculations being done on the variables in my script, so I would just be referencing static variables.

Fair enough. I admit that I didn’t completely understand your script setup here and how everything was working together (or how you wanted it to work together). I do know that your subclasses will show any parent class variables.
Since it’s too difficult to guess at your design any more, I can just wish you good luck and hope you find a workable solution :slight_smile: