Ok, so I have several Scriptable Objects that are policies for governance, and each one has a public Sprite named 'icon' and a category and several float values. In another C# script (a MonoBehaviour) I'm trying to set an Image's sprite to that icon, but in my editor it says "Policy does not contain a definition for 'icon'..." but all the other values come up just fine, just not the Sprite.
Here's my Policy Scriptable Object code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[CreateAssetMenu(fileName = "New Policy", menuName = "Colony/Policy")]
public class Policy : ScriptableObject
{
public string policyName;
public string description;
public enum Category { Health, Happiness, Control, Espionage, Defense, PopulationGrowth, Production, Research, Agriculture, FactionSpecific }
public Category category;
public float happinessModifier;
public float healthModifier;
public float controlModifier;
public float espionageModifier;
public float defenseModifier;
public float productionModifier;
public float researchModifier;
public float agricultureModifier;
public float populationGrowthModifier;
public float diplomacyModifier;
public Faction.FactionID factionID; // FactionID of faction that can use this policy (if faction-specific)
public Sprite icon;
}
And here's the code where I'm trying to access the icon Sprite:
void ApplyPolicyFromDropdown(TMP_Dropdown dropdown)
{
int selectedIndex = dropdown.value;
if (selectedIndex > 0) // Ignore "Select Policy"
{
Policy selectedPolicy = dropdownPolicies[dropdown][selectedIndex - 1]; // Offset by 1
policyImages[selectedPolicy.category].sprite = selectedPolicy.icon; //here is where my code breaks - says does not contain definition for 'icon'
policyManager.ApplyPolicy(selectedPolicy);
}
}
Ok, so I have several Scriptable Objects that are policies for governance, and each one has a public Sprite named 'icon' and a category and several float values. In another C# script (a MonoBehaviour) I'm trying to set an Image's sprite to that icon, but in my editor it says "Policy does not contain a definition for 'icon'..." but all the other values come up just fine, just not the Sprite.
Here's my Policy Scriptable Object code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[CreateAssetMenu(fileName = "New Policy", menuName = "Colony/Policy")]
public class Policy : ScriptableObject
{
public string policyName;
public string description;
public enum Category { Health, Happiness, Control, Espionage, Defense, PopulationGrowth, Production, Research, Agriculture, FactionSpecific }
public Category category;
public float happinessModifier;
public float healthModifier;
public float controlModifier;
public float espionageModifier;
public float defenseModifier;
public float productionModifier;
public float researchModifier;
public float agricultureModifier;
public float populationGrowthModifier;
public float diplomacyModifier;
public Faction.FactionID factionID; // FactionID of faction that can use this policy (if faction-specific)
public Sprite icon;
}
And here's the code where I'm trying to access the icon Sprite:
void ApplyPolicyFromDropdown(TMP_Dropdown dropdown)
{
int selectedIndex = dropdown.value;
if (selectedIndex > 0) // Ignore "Select Policy"
{
Policy selectedPolicy = dropdownPolicies[dropdown][selectedIndex - 1]; // Offset by 1
policyImages[selectedPolicy.category].sprite = selectedPolicy.icon; //here is where my code breaks - says does not contain definition for 'icon'
policyManager.ApplyPolicy(selectedPolicy);
}
}
Share
asked Mar 9 at 19:17
Jacob JacksonJacob Jackson
3643 silver badges13 bronze badges
2
- Try changing from if (selectedIndex > 0) to if (selectedIndex >= 0) In c# index can be zero. – jdweng Commented Mar 9 at 19:52
- @jdweng but if it were 0, his -1 would make it invalid :D – BugFinder Commented Mar 9 at 19:56
1 Answer
Reset to default 0Turns out all I had to do was restart my editor - Visual Studio wasn't seeing the changes in the Policy Scriptable Object for some reason.