Issue with toggles in C# Unity

I am working on a character generator for an RPG, and I have a section of code where only half of the toggles are working properly. For example, when the value of toggleChaste changes, if the toggle is on then the text of toggleChasteLabel should change to “Chaste 16,” and correspondingly toggleLustfulLabel should change to “Lustful 4.” If the toggle is off after it changes, then it should show a value assigned elsewhere in the program (or the default of 10) for each side. These are also all part of a toggle group, such that when another toggle is turned, if any others were on they turn off.

All of the toggles on the right side (Lustful, Lazy, Cowardly etc.) work fine, while the ones on the left (Chaste, Energetic, Valorous etc.) do not change the text when they are toggled. I cannot figure out why this is happening. It seems that all of the toggles and labels are serialized and assigned properly. Here is my UI and the PassionUpdate() method from my Game script that runs when the value of one of the toggles changes.

Please let me know if more information is needed, or if this is the wrong place to ask this question. I have been returning to this part of the program repeatedly as I work on other sections, and I simply cannot figure out what the problem is, and why half of them would be working while the others are not. Thanks!

[SerializeField] Toggle toggleChaste;
[SerializeField] Toggle toggleLustful;
[SerializeField] Toggle toggleEnergetic;
[SerializeField] Toggle toggleLazy;
[SerializeField] Toggle toggleForgiving;
[SerializeField] Toggle toggleVengeful;
[SerializeField] Toggle toggleGenerous;
[SerializeField] Toggle toggleSelfish;
[SerializeField] Toggle toggleHonest;
[SerializeField] Toggle toggleDeceitful;
[SerializeField] Toggle toggleJust;
[SerializeField] Toggle toggleArbitrary;
[SerializeField] Toggle toggleMerciful;
[SerializeField] Toggle toggleCruel;
[SerializeField] Toggle toggleModest;
[SerializeField] Toggle toggleProud;
[SerializeField] Toggle togglePrudent;
[SerializeField] Toggle toggleReckless;
[SerializeField] Toggle toggleSpiritual;
[SerializeField] Toggle toggleWorldly;
[SerializeField] Toggle toggleTemperate;
[SerializeField] Toggle toggleIndulgent;
[SerializeField] Toggle toggleTrusting;
[SerializeField] Toggle toggleSuspicious;
[SerializeField] Toggle toggleValorous;
[SerializeField] Toggle toggleCowardly;

[SerializeField] Text toggleChasteLabel;
[SerializeField] Text toggleLustfulLabel;
[SerializeField] Text toggleEnergeticLabel;
[SerializeField] Text toggleLazyLabel;
[SerializeField] Text toggleForgivingLabel;
[SerializeField] Text toggleVengefulLabel;
[SerializeField] Text toggleGenerousLabel;
[SerializeField] Text toggleSelfishLabel;
[SerializeField] Text toggleHonestLabel;
[SerializeField] Text toggleDeceitfulLabel;
[SerializeField] Text toggleJustLabel;
[SerializeField] Text toggleArbitraryLabel;
[SerializeField] Text toggleMercifulLabel;
[SerializeField] Text toggleCruelLabel;
[SerializeField] Text toggleModestLabel;
[SerializeField] Text toggleProudLabel;
[SerializeField] Text togglePrudentLabel;
[SerializeField] Text toggleRecklessLabel;
[SerializeField] Text toggleSpiritualLabel;
[SerializeField] Text toggleWorldlyLabel;
[SerializeField] Text toggleTemperateLabel;
[SerializeField] Text toggleIndulgentLabel;
[SerializeField] Text toggleTrustingLabel;
[SerializeField] Text toggleSuspiciousLabel;
[SerializeField] Text toggleValorousLabel;
[SerializeField] Text toggleCowardlyLabel;

public void PassionUpdate()
{
// Chaste toggled
if (toggleChaste.isOn)
{
toggleChasteLabel.text = “Chaste 16”;
toggleLustfulLabel.text = “Lustful 4”;
}
else if (toggleChaste.isOn == false)
{
toggleChasteLabel.text = "Chaste " + you.chaste.ToString();
toggleLustfulLabel.text = "Lustful " + you.lustful.ToString();
}
// Lustful toggled
if (toggleLustful.isOn)
{
toggleLustfulLabel.text = “Lustful 16”;
toggleChasteLabel.text = “Chaste 4”;
}
else if (toggleLustful.isOn == false)
{
toggleLustfulLabel.text = "Lustful " + you.lustful.ToString();
toggleChasteLabel.text = "Chaste " + you.chaste.ToString();
}
// Energetic toggled
if (toggleEnergetic.isOn)
{
toggleEnergeticLabel.text = “Energetic 16”;
toggleLazyLabel.text = “Lazy 16”;
}
else if (toggleEnergetic.isOn == false)
{
toggleEnergeticLabel.text = "Energetic " + you.energetic.ToString();
toggleLazyLabel.text = "Lazy " + you.lazy.ToString();
}
// Lazy toggled
if (toggleLazy.isOn)
{
toggleLazyLabel.text = “Lazy 16”;
toggleEnergeticLabel.text = “Energetic 4”;
}
else if (toggleLazy.isOn == false)
{
toggleLazyLabel.text = "Lazy " + you.lazy.ToString();
toggleEnergeticLabel.text = "Energetic " + you.energetic.ToString();
}
// Forgiving toggled
if (toggleForgiving.isOn)
{
toggleForgivingLabel.text = “Forgiving 16”;
toggleVengefulLabel.text = “Vengeful 4”;
}
else if (toggleForgiving.isOn == false)
{
toggleForgivingLabel.text = "Forgiving " + you.forgiving.ToString();
toggleVengefulLabel.text = "Vengeful " + you.vengeful.ToString();
}
// Vengeful toggled
if (toggleVengeful.isOn)
{
toggleForgivingLabel.text = “Forgiving 4”;
toggleVengefulLabel.text = “Vengeful 16”;
}
else if (toggleVengeful.isOn == false)
{
toggleVengefulLabel.text = "Vengeful " + you.vengeful.ToString();
toggleForgivingLabel.text = "Forgiving " + you.forgiving.ToString();
}
// Generous toggled
if (toggleGenerous.isOn)
{
toggleGenerousLabel.text = “Generous 16”;
toggleSelfishLabel.text = “Selfish 4”;
}
else if (toggleGenerous.isOn == false)
{
toggleGenerousLabel.text = "Generous " + you.generous.ToString();
toggleSelfishLabel.text = "Selfish " + you.selfish.ToString();
}
// Selflish toggled
if (toggleSelfish.isOn)
{
toggleSelfishLabel.text = “Selfish 16”;
toggleGenerousLabel.text = “Generous 4”;
}
else if (toggleSelfish.isOn == false)
{
toggleSelfishLabel.text = "Selfish " + you.selfish.ToString();
toggleGenerousLabel.text = "Generous " + you.generous.ToString();
}
// Honest toggled
if (toggleHonest.isOn)
{
toggleHonestLabel.text = “Honest 16”;
toggleDeceitfulLabel.text = “Deceitful 4”;
}
else if (toggleHonest.isOn == false)
{
toggleHonestLabel.text = "Honest " + you.honest.ToString();
toggleDeceitfulLabel.text = "Deceitful " + you.deceitful.ToString();
}
// Deceitful toggled
if (toggleDeceitful.isOn)
{
toggleDeceitfulLabel.text = “Deceitful 16”;
toggleHonestLabel.text = “Honest 4”;
}
else if (toggleDeceitful.isOn == false)
{
toggleHonestLabel.text = "Honest " + you.honest.ToString();
toggleDeceitfulLabel.text = "Deceitful " + you.deceitful.ToString();
}
// Just toggled
if (toggleJust.isOn)
{
toggleJustLabel.text = “Just 16”;
toggleArbitraryLabel.text = “Arbitrary 4”;
}
else if (toggleJust.isOn == false)
{
toggleJustLabel.text = "Just " + you.just.ToString();
toggleArbitraryLabel.text = "Arbitrary " + you.arbitrary.ToString();
}
// Arbitrary toggled
if (toggleArbitrary.isOn)
{
toggleArbitraryLabel.text = “Arbitrary 16”;
toggleJustLabel.text = “Just 4”;
}
else if (toggleArbitrary.isOn == false)
{
toggleArbitraryLabel.text = "Arbitrary " + you.arbitrary.ToString();
toggleJustLabel.text = "Just " + you.just.ToString();
}
// Merciful toggled
if (toggleMerciful.isOn)
{
toggleMercifulLabel.text = “Merciful 16”;
toggleCruelLabel.text = “Cruel 4”;
}
else if (toggleMerciful.isOn == false)
{
toggleMercifulLabel.text = "Merciful " + you.merciful.ToString();
toggleCruelLabel.text = "Cruel " + you.cruel.ToString();
}
// Cruel toggled
if (toggleCruel.isOn)
{
toggleCruelLabel.text = “Cruel 16”;
toggleMercifulLabel.text = “Merciful 4”;
}
else if (toggleCruel.isOn == false)
{
toggleCruelLabel.text = "Cruel " + you.cruel.ToString();
toggleMercifulLabel.text = "Merciful " + you.merciful.ToString();
}
// Modest toggled
if (toggleModest.isOn)
{
toggleModestLabel.text = “Modest 16”;
toggleProudLabel.text = “Proud 4”;
}
else if (toggleModest.isOn == false)
{
toggleModestLabel.text = "Modest " + you.modest.ToString();
toggleProudLabel.text = "Proud " + you.proud.ToString();
}
// Proud toggled
if (toggleProud.isOn)
{
toggleProudLabel.text = “Proud 16”;
toggleModestLabel.text = “Modest 4”;
}
else if (toggleProud.isOn == false)
{
toggleProudLabel.text = "Proud " + you.proud.ToString();
toggleModestLabel.text = "Modest " + you.modest.ToString();
}
// Prudent toggled
if (togglePrudent.isOn)
{
togglePrudentLabel.text = “Prudent 16”;
toggleRecklessLabel.text = “Reckless 4”;
}
else if (togglePrudent.isOn == false)
{
togglePrudentLabel.text = "Prudent " + you.prudent.ToString();
toggleRecklessLabel.text = "Reckless " + you.reckless.ToString();
}
// Reckless toggled
if (toggleReckless.isOn)
{
toggleRecklessLabel.text = “Reckless 16”;
togglePrudentLabel.text = “Prudent 4”;
}
else if (toggleReckless.isOn == false)
{
toggleRecklessLabel.text = "Reckless " + you.reckless.ToString();
togglePrudentLabel.text = "Prudent " + you.prudent.ToString();
}
// Spiritual toggled
if (toggleSpiritual.isOn)
{
toggleSpiritualLabel.text = “Spiritual 16”;
toggleWorldlyLabel.text = “Worldly 4”;
}
else if (toggleSpiritual.isOn == false)
{
toggleSpiritualLabel.text = "Spiritual " + you.spiritual.ToString();
toggleWorldlyLabel.text = "Worldly " + you.worldly.ToString();
}
// Worldly toggled
if (toggleWorldly.isOn)
{
toggleWorldlyLabel.text = “Worldly 16”;
toggleSpiritualLabel.text = “Spiritual 4”;
}
else if (toggleWorldly.isOn == false)
{
toggleSpiritualLabel.text = "Spiritual " + you.spiritual.ToString();
toggleWorldlyLabel.text = "Worldly " + you.worldly.ToString();
}
// Temperate toggled
if (toggleTemperate.isOn)
{
toggleTemperateLabel.text = “Temperate 16”;
toggleIndulgentLabel.text = “Indulgent 4”;
}
else if (toggleTemperate.isOn == false)
{
toggleTemperateLabel.text = "Temperate " + you.temperate.ToString();
toggleIndulgentLabel.text = "Indulgent " + you.indulgent.ToString();
}
// Indulgent toggled
if (toggleIndulgent.isOn)
{
toggleIndulgentLabel.text = “Indulgent 16”;
toggleTemperateLabel.text = “Temperate 4”;
}
else if (toggleIndulgent.isOn == false)
{
toggleTemperateLabel.text = "Temperate " + you.temperate.ToString();
toggleIndulgentLabel.text = "Indulgent " + you.indulgent.ToString();
}
// Trusting toggled
if (toggleTrusting.isOn)
{
toggleTrustingLabel.text = “Trusting 16”;
toggleSuspiciousLabel.text = “Suspicious 4”;
}
else if (toggleTrusting.isOn == false)
{
toggleTrustingLabel.text = "Trusting " + you.trusting.ToString();
toggleSuspiciousLabel.text = "Suspicious " + you.suspicious.ToString();
}
// Suspicious toggled
if (toggleSuspicious.isOn)
{
toggleSuspiciousLabel.text = “Suspicious 16”;
toggleTrustingLabel.text = “Trusting 4”;
}
else if (toggleSuspicious.isOn == false)
{
toggleSuspiciousLabel.text = "Suspicious " + you.suspicious.ToString();
toggleTrustingLabel.text = "Trusting " + you.trusting.ToString();
}
// Valorous toggled
if (toggleValorous.isOn)
{
toggleValorousLabel.text = “Valorous 16”;
toggleCowardlyLabel.text = “Cowardly 4”;
}
else if (toggleValorous.isOn == false)
{
toggleValorousLabel.text = "Valorous " + you.valorous.ToString();
toggleCowardlyLabel.text = "Cowardly " + you.cowardly.ToString();
}
// Cowardly toggled
if (toggleCowardly.isOn)
{
toggleCowardlyLabel.text = “Cowardly 16”;
toggleValorousLabel.text = “Valorous 4”;
}
else if (toggleCowardly.isOn == false)
{
toggleCowardlyLabel.text = "Cowardly " + you.cowardly.ToString();
toggleValorousLabel.text = "Valorous " + you.valorous.ToString();
}
}

Oh my goodness… you really really really want to learn about ScriptableObjects… none of that data should be in code in the year 2023!

These things (inventory, shop systems, character customization, crafting, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

Inventory code never lives “all by itself.” All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.

Inventories / shop systems / character selectors all contain elements of:

  • a database of items that you may possibly possess / equip
  • a database of the items that you actually possess / equip currently
  • perhaps another database of your “storage” area at home base?
  • persistence of this information to storage between game runs
  • presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
  • interaction with items in the inventory or on the character or in the home base storage area
  • interaction with the world to get items in and out
  • dependence on asset definition (images, etc.) for presentation

Just the design choices of such a system can have a lot of complicating confounding issues, such as:

  • can you have multiple items? Is there a limit?
  • if there is an item limit, what is it? Total count? Weight? Size? Something else?
  • are those items shown individually or do they stack?
  • are coins / gems stacked but other stuff isn’t stacked?
  • do items have detailed data shown (durability, rarity, damage, etc.)?
  • can users combine items to make new items? How? Limits? Results? Messages of success/failure?
  • can users substantially modify items with other things like spells, gems, sockets, etc.?
  • does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
  • etc.

Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

Breaking down a large problem such as inventory:

If you want to see most of the steps involved, make a “micro inventory” in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

Everything you learn doing that “micro inventory” of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

Breaking down large problems in general:


To debug what you have above, use this process:

Time to start debugging! Here is how you can begin your exciting new debugging adventures:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the names of the GameObjects or Components involved?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

“When in doubt, print it out!™” - Kurt Dekker (and many others)

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.