x


Charater Missing numbers

This script used to work, but something happened as I went through the tutorial. At first, the buttons were too close together and overlapping and you couldn't see the skill points remaining. Now I'm not even getting any values. The more I look at it, the more cross-eyed I become. I need some new eyes on this. Thanks!

using UnityEngine;
using System.Collections;
using System;

public class CharacterGenerator : MonoBehaviour {
    private PlayerCharacter _toon;
    private const int  STARTING_POINTS=350;
    private const int  MINSTARTING_ATTRIBUTE_VALUE= 10;
    private int PointsLeft;
    private const int OFFSET=5;
    private const int LINE_HEIGHT=20;
    private const int STARTING_VALUE=50;
    private const int STAT_LABEL_WIDTH= 100;
    private const int BASEVALUE_LABEL_WIDTH= 30;
    private const int BUTTON_WIDTH=25;
    private const int BUTTON_HEIGHT=25;
    private int statStartingPos=40;
    
    // Use this for initialization
    void Start () {
        PointsLeft=STARTING_POINTS;
    _toon = new PlayerCharacter();
    _toon.Awake();
    
        for(int cnt=0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
            _toon.GetPrimaryAttribute(cnt).BaseValue= STARTING_VALUE;
            PointsLeft-=(STARTING_VALUE - MINSTARTING_ATTRIBUTE_VALUE);
        }
    _toon.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:");    
    _toon.Name=GUI.TextField(new Rect(65, 10, 100, 25), _toon.Name);
    }
    
    private void DisplayAttributes(){
    for(int cnt=0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
    GUI.Label(new Rect(OFFSET,                                                //x pos
                               statStartingPos + (cnt *LINE_HEIGHT),        //y pos
                               STAT_LABEL_WIDTH,                                 //item width
                               LINE_HEIGHT),                                           //item height
                                 ((AttributeName)cnt).ToString() );                  
    GUI.Label(new Rect(STAT_LABEL_WIDTH +OFFSET,     //x pos
                               statStartingPos + (cnt *LINE_HEIGHT),       //y pos
                               BASEVALUE_LABEL_WIDTH,                 //item width
                               LINE_HEIGHT),                                          //item height 
                               _toon.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString() );
    if(GUI.Button(new Rect(OFFSET+STAT_LABEL_WIDTH+BASEVALUE_LABEL_WIDTH, //x pos
                                   statStartingPos + (cnt *BUTTON_HEIGHT),                                                  //y    pos
                                   BUTTON_WIDTH ,                                                                                          //button width
                                   BUTTON_HEIGHT),                                                                                     //button height    
                          " - " )){
                if(_toon.GetPrimaryAttribute(cnt).BaseValue > MINSTARTING_ATTRIBUTE_VALUE){
                _toon.GetPrimaryAttribute(cnt).BaseValue--;
                PointsLeft++;
                    _toon.StatUpdate();
                }
            }
    
    if(GUI.Button(new Rect(OFFSET+STAT_LABEL_WIDTH+BASEVALUE_LABEL_WIDTH+BUTTON_WIDTH, //x pos
                                   statStartingPos + (cnt *BUTTON_HEIGHT),                                                                                     //y pos
                                   BUTTON_WIDTH ,                                                                                                                           //button width    
                                   BUTTON_HEIGHT),                                                                                                                        //button height
                          " + " )){
                if(PointsLeft > 0){
                _toon.GetPrimaryAttribute(cnt).BaseValue++;
                    PointsLeft--;
                    _toon.StatUpdate();
                }
            }
        }
    }
    
    private void DisplayVitals() {
    for(int cnt=0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++) {
    GUI.Label(new Rect(OFFSET,                                                   //x pos
                               statStartingPos + ((cnt +7)*LINE_HEIGHT),  //y pos
                               STAT_LABEL_WIDTH,                                  //item width
                               LINE_HEIGHT),                                            // item height
                      ((VitalName)cnt).ToString() );
    GUI.Label(new Rect(OFFSET+STAT_LABEL_WIDTH,       //x pos
                               statStartingPos + ((cnt +7)*LINE_HEIGHT), //y pos
                               BASEVALUE_LABEL_WIDTH,                     //item width
                               LINE_HEIGHT),                                            //item height
                      _toon.GetVital(cnt).AdjustedBaseValue.ToString() );
        }
    }
    
    private void DisplaySkills() {
    for(int cnt=0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++) {
    GUI.Label(new Rect(OFFSET+STAT_LABEL_WIDTH+BASEVALUE_LABEL_WIDTH+BUTTON_WIDTH*2+OFFSET *2, //x pos
                               statStartingPos + (cnt*LINE_HEIGHT),                                                                                                                //y pos
                               100,                                                                                                                                                                         //item width
                               LINE_HEIGHT),                                                                                                                                                   //item height
                      ((SkillName)cnt).ToString() );
    GUI.Label(new Rect(OFFSET+STAT_LABEL_WIDTH+BASEVALUE_LABEL_WIDTH+BUTTON_WIDTH*2+OFFSET *2+STAT_LABEL_WIDTH,
                               statStartingPos + (cnt*LINE_HEIGHT),                    //y pos
                               BASEVALUE_LABEL_WIDTH,                             //item width
                               LINE_HEIGHT),                                                         //item height
                      _toon.GetSkill(cnt).AdjustedBaseValue.ToString() );            
    }
    }
    private void DisplayPointsLeft() {
            GUI.Label(new Rect(250, 10, 100, 25), "Skill Points Left:" +PointsLeft.ToString());    
        }
}
more ▼

asked Feb 29 '12 at 09:12 PM

Gilead7 gravatar image

Gilead7
117 18 32 39

You really expect us to read all that ? We love our eyes too !

Feb 29 '12 at 10:29 PM Berenger

My. God. Big. Script. First try cutting it down to problem areas, then try formatting it properly in your question. Then we'll take a look.

Mar 01 '12 at 06:31 AM Kleptomaniac

The error I'm getting is a NullReference Exception.

Mar 06 '12 at 05:49 PM Gilead7
(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

This code is very hard to read (too long lines!), but I suspect that _toon is the culprit. What do you wanna do with this line:

_toon = new PlayerCharacter();

What is PlayerCharacter? A script? If so, are you trying to add a PlayerCharacter script to the object? Usually you should use AddComponent(PlayerCharacter) to instantiate and attach the script to the object:

_toon = gameObject.AddComponent< PlayerCharacter>();

But if the object already has a PlayerCharacter script, use GetComponent instead:

_toon = GetComponent< PlayerCharacter>();
more ▼

answered Mar 06 '12 at 11:17 PM

aldonaletto gravatar image

aldonaletto
41.4k 16 42 197

(comments are locked)
10|3000 characters needed characters left
The problem comes down to these two functions: private void DisplaySkills() { for(int cnt=0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++) { GUI.Label(new Rect(OFFSET+STAT_LABEL_WIDTH+BASEVALUE_LABEL_WIDTH+BUTTON_WIDTH*2+OFFSET *2, statStartingPos +(cnt*LINE_HEIGHT),100,LINE_HEIGHT), ((SkillName)cnt).ToString() ); //this part looked suspect to me, but not matter how I tried to fix it, it wouldn't work. GUI.Label(new Rect(OFFSET+STAT_LABEL_WIDTH+BASEVALUE_LABEL_WIDTH+BUTTON_WIDTH*2+OFFSET *2+STAT_LABEL_WIDTH, statStartingPos (cnt*LINE_HEIGHT), BASEVALUE_LABEL_WIDTH, LINE_HEIGHT), _toon.GetSkill(cnt).AdjustedBaseValue.ToString() ); } } Here's the other one: private void DisplayVitals() { for(int cnt=0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++) { GUI.Label(new Rect(OFFSET, statStartingPos + ((cnt +7)*LINE_HEIGHT), STAT_LABEL_WIDTH, LINE_HEIGHT), ((VitalName)cnt).ToString() ); GUI.Label(new Rect(OFFSET+STAT_LABEL_WIDTH, statStartingPos + ((cnt +7)*LINE_HEIGHT), BASEVALUE_LABEL_WIDTH, LINE_HEIGHT), _toon.GetVital(cnt).AdjustedBaseValue.ToString() ); } }
more ▼

answered Mar 06 '12 at 11:30 PM

Gilead7 gravatar image

Gilead7
117 18 32 39

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4152
x317

asked: Feb 29 '12 at 09:12 PM

Seen: 366 times

Last Updated: Mar 06 '12 at 11:30 PM