[SOLVED] Get/Set doesn't work with a static instance of a class?

Hi there -

In Start(), I create a static instance of the class that stores some of my player attributes. While I can successfully store, save, and load changes (e.g., attributes.jar ++ would correctly increment the value), what it will not do is trigger a Debug.Log line. Any ideas why? Code below.

public class PlayerController : MonoBehaviour {

public static Attributes attributes;

void Start()
        {
        if (attributes == null) { attributes = new Attributes(); }
        }
}
[System.Serializable]
public class Attributes
    {
    public double jar;
    private double _jar
        {
        get
            {
            return _jar;
            }
        set
            {
            _jar = value;
            Debug.Log("The new value is " + jar);
            }
        }

    public Attributes()
        {
        jar = 0;
        }
    }

I think you need to include “using UnityEngine;” in that class/script/file?

You sure you haven’t turned off messages in the debugger?

It’s there - it’s definitely there; sorry, I edited the contents for brevity. Here is the full version if of relevance:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Attributes
    {

    #region === Offline Income ===
    [Tooltip("The player's offline candy base, used for computing offline income.")]
    [SerializeField]
    public double jar;
    private double _jar
        {
        get
            {
            return _jar;
            }
        set
            {
            _jar = value;
            Debug.Log("The new value is " + jar);
            }
        }

    [Tooltip("The percentage of candy value to be added to the offline income base upon gaining money.")]
    [SerializeField]
    public float jarCollectFactor;

    [Tooltip("The percentage of the hoard to pay out.")]
    [SerializeField]
    public float jarPayoutFactor;

    [Tooltip("How much the user earns per second, based on a five-second average. Used for calculating offline gains.")]
    [SerializeField]
    private double _incomePerSecond;
    public double incomePerSecond
        {
        get
            {
            return _incomePerSecond;
            }
        set
            {
            _incomePerSecond = value;
            }
        }
    #endregion

    public Attributes()
        {
        jar = 0;
        jarCollectFactor = 0.01f;
        jarPayoutFactor = 0.005f;
        incomePerSecond = 0;
        }
    }

There are essentially two potential options for ‘Debug.Log()’

The first is ‘using System.Diagnostics’
The other is ‘using UnityEngine’

Check your imports on which one you are using (it needs to be what methos5k suggested for it to appear in the editor logs)

edit
nevermind… out of curiousity though, try logwarning or logerror. see if they give anything

Absolutely - in fact, if I call Debug.Log from anywhere else (e.g., from PlayerController), it works perfectly. For example, this line in PlayerController would work just as it normally should:

void Foo ()
{
attributes.jar++;
Debug.Log(attributes.jar);
}

Sorry, then I am not sure :slight_smile:

Weird. The only other time I’ve seen Debug.Log not do anything is if you call it from off the main thread. It doesn’t look like this is happening here.

Wait… wait… I think maybe I see the problem.
OP is/was saying that attributes.jar++ works/increments… but that is a public variable.
the ‘_jar’ is private. and is never used.
Also, I do not think _jar is setup properly.
Anyways, do you ever call _jar++ or _jar = (anything) ?

Usually you’d want:

private double _jar;
public double jar { get { return _jar; } set { _jar = value; } }
4 Likes

…

/facepalm

…

I, uh, suppose, um, that’s what I deserve for coding at 5AM without so much as an espresso?

hangs his head in shame

Go ahead, move along, nothing to see here…

Thank you, Methos!

2 Likes

:slight_smile: Cheers. You’re welcome.

Whoops. The good news is you caught it. If you’d actually called that property, it would have locked you in an infinite loop. Maybe with a stack overflow to get you out.