hello again @Brian_Trotter - I need a little bit of help
- OK so here’s the problem:
I’m trying to activate and deactivate a quantity counter for my crafting system, so that it only works if, and only if, you’re using a farming patch (remember when I told you I mixed my Construction and Crafting Systems to come up with a farming system?), and here’s the (somewhat minor) problem:
The system works based on an ‘OnEnable()’ and ‘OnDisable()’ function, and through countless try and errors, this is as clean as it will ever get:
void OnEnable()
{
// Happens for a millisecond when the game scene starts, and then disabled
// by 'OnDisable', unless commanded by the 'Crafting UI' to open up
Debug.Log($"Quantity Controller has been enabled");
// Initialize the Quantity to the minimum value
currentQuantity = minQuantity;
UpdateQuantityText();
// Add listeners to the buttons
plusButton.onClick.AddListener(AddQuantity);
minusButton.onClick.AddListener(SubtractQuantity);
// Buttons' Image Components
plusButtonImage = plusButton.GetComponent<Image>();
minusButtonImage = minusButton.GetComponent<Image>();
// Update button transparency
UpdateButtonState();
// The Crafting Table is opened by finding the nearest Crafting Table, so this approach just fits perfectly
currentCraftingTable = craftingFinder.GetNearestTable();
// Only activate the Quantity Controller if, and only if, the table near you is
// a farming table (i.e: it has a 'PlantGrowthHandler' as a child gameObject):
plantGrowthHandler = currentCraftingTable?.GetComponentInChildren<PlantGrowthHandler>();
if (plantGrowthHandler == null)
{
Debug.Log($"Current Crafting Table has no Plant Growth Handler. Disabling QuantityController");
this.gameObject.SetActive(false); // will go to 'OnDisable()'
return;
}
}
void OnDisable()
{
// Quickly disables 'OnEnable' when the game scene starts,
// and also called when 'Crafting UI' Quit Button commands a shut down
Debug.Log($"Quantity Controller has been disabled");
plusButton.onClick.RemoveListener(AddQuantity);
plusButton.onClick.RemoveListener(SubtractQuantity);
if (!this.gameObject.activeSelf)
{
this.gameObject.SetActive(true);
}
}
(I don’t mind sharing the full script, but I didn’t want to overwhelm anyone)
This script is called ‘QuantityController.cs’, and it goes right on top of the Parent that controls the UI of the quantities we are controlling
The problem I have is, is that Unity each time I open a non-farming patch (i.e: ‘PlantGrowthHandler’ is not null), it’ll complain that the code is going to ‘OnDisable()’ really quickly, and it kinda makes sense tbh. Here’s what the error looks like:
GameObject is already being activated or deactivated.
UnityEngine.StackTraceUtility:ExtractStackTrace ()
QuantityController:OnDisable () (at Assets/Asset Packs/bixarrio/RPG Crafting System/Scripts/UI/QuantityController.cs:73)
QuantityController:OnEnable () (at Assets/Asset Packs/bixarrio/RPG Crafting System/Scripts/UI/QuantityController.cs:61)
RPG.Crafting.CraftingSystem:OnCraftingInteraction (RPG.Crafting.ICraftingTable) (at Assets/Asset Packs/bixarrio/RPG Crafting System/Scripts/CraftingSystem.cs:170)
RPG.Crafting.CraftingMediator:NotifyInteraction (RPG.Crafting.ICraftingTable) (at Assets/Asset Packs/bixarrio/RPG Crafting System/Scripts/CraftingMediator.cs:17)
RPG.Crafting.CraftingTable:NotifyCrafting () (at Assets/Asset Packs/bixarrio/RPG Crafting System/Scripts/CraftingTable.cs:90)
RPG.States.Player.PlayerCraftingState:Enter () (at Assets/Asset Packs/bixarrio/RPG Crafting System/Scripts/PlayerCraftingState.cs:29)
RPG.States.StateMachine:SwitchState (RPG.States.State) (at Assets/Project Backup/Scripts/State Machines/StateMachine.cs:13)
RPG.States.Player.PlayerFacingState:Tick (single) (at Assets/Project Backup/Scripts/State Machines/Player/PlayerFacingState.cs:35)
RPG.States.StateMachine:Update () (at Assets/Project Backup/Scripts/State Machines/StateMachine.cs:18)
Do you know of an alternative solution to go around this? (I tried modifying it from bixarrio’s CraftingSystem.cs script, which looks exactly like the copy he gave you, but it needed that ‘extra step’ of opening and shutting it down once, before it worked again. So, this is the most accurate, but troublesome solution, I can get so far)
Any idea how to fix this? (I didn’t get to the point of making this actually work just yet)