Hi guys i hv 2 scripts connected, they work almost 100% fine but i hv an issue, in BaseCharacter one i calculate the Vital and Skill Modifier but when i want them to actualize in character creation with the points i give in my status they always show 0 at all the skills and vital has show the image, can anyone help me figure it out wt im doing wrong plz? ty Code (CSharp): using UnityEngine; using System.Collections; using System; //added to acces enum class public class BaseCharacter : MonoBehaviour { private string _name; private int _level; private uint _freeExp; //only assign possitive values private AttributeClass[] _primaryAttribute; private VitalClass[] _vital; private Skill[] _skill; public void Awake() { _name = string.Empty; _level = 0; _freeExp = 0; _primaryAttribute = new AttributeClass[Enum.GetValues(typeof(AttributeName)).Length]; _vital = new VitalClass[Enum.GetValues(typeof(VitalName)).Length]; _skill = new Skill[Enum.GetValues(typeof(SkillName)).Length]; SetupPrimaryAttributes (); SetupVitals (); SetupSkills (); } public string Name { get{return _name;} set{_name = value;} } public int Level { get{return _level;} set{_level = value;} } public uint FreeExp { get{return _freeExp;} set{_freeExp = value;} } public void AddExp(uint exp) { _freeExp += exp; CalculateLevel (); } //take average of all of the player skills and assign that as the player level public void CalculateLevel() { } private void SetupPrimaryAttributes() { for (int cnt = 0; cnt < _primaryAttribute.Length; cnt++) { _primaryAttribute[cnt] = new AttributeClass(); } } private void SetupVitals() { for (int cnt = 0; cnt < _vital.Length; cnt++) _vital[cnt] = new VitalClass(); SetupVitalModifiers (); } private void SetupSkills() { for (int cnt = 0; cnt < _skill.Length; cnt++) _skill[cnt] = new Skill (); SetupSkillModifiers (); } public AttributeClass GetPrimaryAttribute(int index) { return _primaryAttribute [index]; } public VitalClass GetVital(int index) { return _vital [index]; } public Skill GetSkill(int index) { return _skill [index]; } private void SetupVitalModifiers() { //health GetVital ((int)VitalName.HEALTH).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.CONSTITUTION), .5f)); //energy GetVital ((int)VitalName.ENERGY).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.CONSTITUTION), 1)); //mana GetVital ((int)VitalName.MANA).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.MAGIC), 1)); } private void SetupSkillModifiers() { //melee offence GetSkill ((int)SkillName.MELEE_OFFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.MIGHT), .33f)); GetSkill ((int)SkillName.MELEE_OFFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.MAGIC), .33f)); //melee defence GetSkill ((int)SkillName.MELEE_DEFENSE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.SPEED), .33f)); GetSkill ((int)SkillName.MELEE_DEFENSE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.CONSTITUTION), .33f)); //magic offence GetSkill ((int)SkillName.MAGIC_OFFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.CONCENTRATION), .33f)); GetSkill ((int)SkillName.MAGIC_OFFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.WILLPOWER), .33f)); //mafic defence GetSkill ((int)SkillName.MAGIC_DEFENSE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.CONCENTRATION), .33f)); GetSkill ((int)SkillName.MAGIC_DEFENSE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.WILLPOWER), .33f)); //ranged offence GetSkill ((int)SkillName.RANGED_OFFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.CONCENTRATION), .33f)); GetSkill ((int)SkillName.RANGED_OFFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.SPEED), .33f)); //ranged defence GetSkill ((int)SkillName.RANGED_DEFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.SPEED), .33f)); GetSkill ((int)SkillName.RANGED_DEFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.MAGIC), .33f)); } public void StatUpdate() { for (int cnt = 0; cnt < _vital.Length; cnt++) _vital [cnt].Update (); for (int cnt = 0; cnt < _skill.Length; cnt++) _skill [cnt].Update (); } } Code (CSharp): using UnityEngine; using System.Collections; using System; public class CharacterGenerator : MonoBehaviour { private PlayerCharacter _player; private const int STARTING_POINTS = 350; private const int MIN_SARTING_ATTRIBUTE_VALUE = 10; private const int STARTING_VALUE = 50; private int pointsLeft; // Use this for initialization public void Start () { _player = new PlayerCharacter (); _player.Awake (); pointsLeft = STARTING_POINTS; for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) { _player.GetPrimaryAttribute(cnt).BaseValue = STARTING_VALUE; pointsLeft -= (STARTING_VALUE - MIN_SARTING_ATTRIBUTE_VALUE); } _player.StatUpdate (); } // Update is called once per frame void Update () { } void OnGUI() { DisplayName (); DisplayPointsLeft (); DisplayAttributes (); DisplayVitals (); DisplaySkills (); } private void DisplayName() { GUI.Label(new Rect(10,10,50,25), "Name:" ); _player.Name = GUI.TextField (new Rect (65,10,100,25), _player.Name); } private void DisplayAttributes() { for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) { GUI.Label (new Rect(10,40+(cnt * 25),120,25), ((AttributeName)cnt).ToString()); GUI.Label (new Rect(150,40+(cnt * 25),30,25), _player.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString()); if(GUI.Button(new Rect(170, 40+(cnt * 25),25,25), "-")) { if(_player.GetPrimaryAttribute(cnt).BaseValue > MIN_SARTING_ATTRIBUTE_VALUE) { _player.GetPrimaryAttribute(cnt).BaseValue--; pointsLeft++; _player.StatUpdate (); } } if(GUI.Button(new Rect(200, 40+(cnt * 25),25,25), "+")) { if(pointsLeft > 0) { _player.GetPrimaryAttribute(cnt).BaseValue++; pointsLeft--; _player.StatUpdate (); } } } } private void DisplayVitals() { for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++) { GUI.Label (new Rect(10,40+((cnt+7) * 25),120,25), ((VitalName)cnt).ToString()); GUI.Label (new Rect(150,40+((cnt+7) * 25),30,25), _player.GetVital(cnt).AdjustedBaseValue.ToString()); } } private void DisplaySkills() { for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++) { GUI.Label (new Rect(250,40+(cnt * 25),130,25), ((SkillName)cnt).ToString()); GUI.Label (new Rect(400,40+(cnt * 25),30,25), _player.GetSkill(cnt).AdjustedBaseValue.ToString()); } } private void DisplayPointsLeft() { GUI.Label(new Rect(250,10,100,25), "Points Left: " + pointsLeft.ToString()); } }
If PlayerCharacter, VitalClass, AttributeClass, etc., are MonoBehaviours, you can't create them with new. Use AddComponent<T>(). Or, better yet, add them in the inspector at design time and use GetComponent<T>() to find them.
Yap but queston is not that warning... the question is the Health Energy Mana and right status should go up with up of main status ex: Constitution increase health but they keep at 0... same for all the others. increase main status should increase the others in % SetupVitalModifiers(); SetupSkilModifiers();
The reason why I mentioned this is because if they're non-MonoBehaviour subclasses, the data will not be serialized unless you mark them with the [System.Serializable] attribute. If that's not the issue, run through SetupVitals() step by step in MonoDevelop's debugger. Here's a tutorial. You can add watches or examine the locals to determine where it's failing to set the values you expect.
seems the code has no errors i will go for serialization process and see if i get it to work.. maybe could be a function called out of place too?
The serialization dont fix the issue... I dont know where im failing this... i re-chack the code 5 times and i dont get it...
Step through the debugger, watching the values in MonoDevelop to make sure they change correctly where you expect them to. If they appear to be correct in SetupSkillModifiers(), then maybe the problem is in DisplaySkills(). Step through the debugger here, too, and make sure that this is returning the correct value: Code (csharp): _player.GetSkill(cnt).AdjustedBaseValue
Omfg.. i found it without debugg... When i use AdjustedBaseValue is AdjustBaseValue... i hv bouth and i trade them and call them wrongly..... Ty m8. Can i ask u one thing? U know a good place to learn c# any kind of book or something i really need improve a lot of my skills... Again ty for ur preciouse help. _O_
Happy to help. Just practice a lot and look at other people's code. Some code is well-written, some isn't. You can learn from bad code, too. You just learn what not to do. There's a free C# tutorial series here. Regardless of language, it's good to read a lot about software engineering principles. Code Complete is a good book for that, as well as Design Patterns. There's also a different design patterns book for C#, but I recommend the original book. And have fun!
I will go for it... really need learn this to do it at least well.. not perfect but well for now Appreciate ur time with me m8 (y)