I'm trying to code some script with Unity, and I have some issue to understand how struct works.
Start with a base of code:
public class Engine : MonoBehaviour {
public Hero selectedHero;
public List<Hero> heroes;
public struct Hero {
public string name;
public Hero(string n) {
name = n;
}
}
}
First I try to make some function to select/unselect..
/* ... */
public Hero getSelected(Hero h) {
return selectedHero;
}
public void setSelected(Hero h) {
selectedHero = h;
}
public bool hasSelected(Hero h) {
return (selectedHero != null);
}
public void clearSelected() {
selectedHero = null; // This is not working ! :'(
}
/* ... */
And I got this error:
Cannot convert null to
Hero
because it is a value type
I read a lot's about C# and Unity Scripting and the answer is:
A struct can't be null in the same way that an int can't be null, and a float can't be null - they're all value types
But ? What's the real solution !? I used two ugly solution to avoid this:
Solution #1 I can put a public bool hasSelected
and always test this one before use the selected
attribute.
Solution #2 Is to create a List<Hero> selected
instead of simple Hero
and handle if the Length is 0 or 1.
Does exist a better solution, more clean ?!
Bonus question: How to create a function who return a single Hero based on a test, my best (ugly) solution:
public Hero GetHero(string name) {
foreach (Hero hero in heroes) {
if (hero.name == name) {
return hero;
}
}
return null; // Not working ?! What can I return ?!
}