- Home /
singleton pointer and updating variables of the singleton with the pointer
When i have created a player class and created a singleton in it:
public static Player instance;
void Awake()
{
if (instance != null)
{
Debug.LogWarning("More than one instance of Inventory found");
return;
}
instance = this;
}
one variable in Player class is playerData and in player data where is a variabale inventory (array of items). in the inventoryUpdate scrip i want to change the items in the inventory that is in the player class . I don't want to call the class every time i want to change (not to call Player.playerData.inventory every time) I tried to point on the inventory variable and call it on Create local variable inventory and that will point on inventory in Start(){inventory = Player.playerData.inventory;} but the playerData.inventoryin in the player class is not changing when i try to change it on the inventoryUpdate with the local pointer inventory: inentory[0] = newItem("sword") and it is changing only the local pointer and not the original variable. I thought that if i never defined a new instance (never used new word) it is only a pointer and it will change the original variable. is there any way to do it? defined a pointer correctly?
It's far from clear that the inventory issue is anything at all to do with the singleton. Like the others have said, you need to show us the code that relates to the inventory
object. What is it? How and when are you creating/modifying it? (both the actual object and the cached pointer to it)
Having said that, the singleton use does look odd from what you've shown. You're accessing playerData
as if it's a static property of the Player class. You've not shown us anywhere that you're actually accessing the singleton (ie via Player.instance). So, again, there's nothing to connect the code or the question title with the problem you're describing.