- Home /
How to properly change variables of a sub class
Hi! I wrote some code with a class and a subclass:
public class Units : MonoBehaviour
{
public int movementPoints = 0;
public int HP = 0;
}
and the subclass:
public class IceBreaker : Units
{
private void Awake()
{
HP = 100;
movementPoints = 2;
}
}
now in a different piece of code I have the function:
public void AddShip(Units unit)
{
print(unit.GetType());
print(unit.HP);
}
the unit.GetType() will always result in IceBreaker when I print it, but unit.HP will always result in 0. The inspector says the HP of my Icebreaker is 100, however. I'm assuming that unit.HP results in the code grabbing the HP value of the Unit class, but I have no clue how to fix this.
You put the changes in the script under the Awake() function; are you enabling the GameObject before you run "AddShip()" or have the values not had a chance to update yet?
Never$$anonymous$$d, I made a stupid mistake where I put the prefab in the addship() instead of the Unit I instantiated. Took me more than an hour to find that mistake haha
Your answer
Follow this Question
Related Questions
How do I save an array of all different variables? 0 Answers
Can I choose what to pass into .getcomponent<{VARIABLE}>(); 1 Answer
Linking prefabs to classes that are not attatched to gameobjects 1 Answer
View script variables within an array of scripts in Inspector 1 Answer
class design preference 1 Answer