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;
}
}
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;
}
}
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:
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) ?
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.