Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

NullReferenceError

Discussion in 'Scripting' started by Regless, Feb 8, 2011.

  1. Regless

    Regless

    Joined:
    Feb 8, 2011
    Posts:
    17
    Hello. I have been trying to figure out unity with the help of BurgZeg's delightful tutorial series. However as of 2 days ago I've hit a dead end. And I cannot figure this once out. the error message is~

    I've gone back and forth through all the lines referenced in the error and even rebuilt this thing one. I am at my wits end so any help would be greatly appreciated. Thank you kindly in advance.

    PHP:
    void OnGUI() {
        
        
    GUI.skin mySkin;
        
        
    DisplayName();
        
    DisplayPointsLeft ();
        
    DisplayAttributes();
        
        
    DisplayVitals ();
        
        
    DisplaySkills ();
        
        
    DisplayCreateButton();

        
        }
        
    private 
    void DisplayName() {
            
    GUI.Label(new Rect(101010025), "Name:");
        
    _toon.Name GUI.TextField(new Rect(65,1010025), _toon.Name);
        }
        
        private 
    void DisplayAttributes () {
        
            for(
    int cnt 0cnt Enum.GetValues(typeof(AttributeName)).Lengthcnt++) {
            
    GUI.Label(new Rect
                
    (OFFSET,                                                                                 //x
                
    statStartingPos + (cnt LINE_HEIGHT),                                         //y
                
    STAT_LABEL_WIDTH,                                                                 //width
                
    LINE_HEIGHT),                                                                         //height
                
    ((AttributeName)cnt).ToString());
                
            
    GUI.Label(new Rect(STAT_LABEL_WIDTH OFFSET,                         //x
                
    statStartingPos + (cnt LINE_HEIGHT),                                         //y
                
    BASE_VALUELABEL_WIDTH,                                                         //width
                
    LINE_HEIGHT),                                                                         //height
                
    _toon.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString());
                
                if(
    GUIButton(new Rect(OFFSET STAT_LABEL_WIDTH BASE_VALUELABEL_WIDTH,     //x
                    
    statStartingPos + (cnt BUTTON_HEIGHT),                                                            //y
                
    BUTTON_WIDTH,                                                                                                     //width
                
    BUTTON_HEIGHT),                                                                                                //height
                
    "-")) {
                    if(
    _toon.GetPrimaryAttribute(cnt).BaseValue  MIN_STARTING_ATTRIBUTE_VALUE) {
                    
    _toon.GetPrimaryAttribute(cnt).BaseValue--;
                    
    pointsLeft ++;
                        
    _toon.StatUpdate();
                    }
                }
                if(
    GUIButton(new Rect(OFFSET STAT_LABEL_WIDTH BASE_VALUELABEL_WIDTH BUTTON_WIDTH,     //x
                    
    statStartingPos + (cnt BUTTON_HEIGHT),                                                                                    //y
                
    BUTTON_WIDTH,                                                                                                                             //width
                
    BUTTON_HEIGHT),                                                                                                                        //height
                
    "+")) {
                    
                    if(
    pointsLeft 0) {
                        
    _toon.GetPrimaryAttribute(cnt).BaseValue++;
                        
    pointsLeft --;
                        
                    
    _toon.StatUpdate();
                    }
                }
            }
        }
                    
                    private 
    void DisplayVitals () {
                for(
    int cnt 0cnt Enum.GetValues(typeof(VitalName)).Lengthcnt++) {
            
    GUI.Label(new Rect(OFFSET,                                                                     //x
                    
    statStartingPos + ((cnt 7) * LINE_HEIGHT),                                        //y
                    
    STAT_LABEL_WIDTH,                                                                         //width
                    
    LINE_HEIGHT),                                                                                 //height
                    
    ((VitalName)cnt).ToString() );
                    
            
    GUI.Label(new Rect(OFFSET STAT_LABEL_WIDTH,                                     //x
                    
    40 + ((cnt 7) * LINE_HEIGHT),                                                         //y
                    
    BASE_VALUELABEL_WIDTH,                                                                 //width
                    
    LINE_HEIGHT),                                                                                 //height
                    
    _toon.GetVital(cnt).AdjustedBaseValue.ToString());
            }
        }
            private 
    void DisplaySkills () {
                for(
    int cnt 0cnt Enum.GetValues(typeof(SkillName)).Lengthcnt++) {
            
    GUI.Label(new Rect(OFFSET 4STAT_LABEL_WIDTH BASE_VALUELABEL_WIDTH BUTTON_WIDTH 2,     //x
                    
    statStartingPos + (cnt LINE_HEIGHT),                                                                                         //y
                    
    STAT_LABEL_WIDTH,                                                                                                                 //width
                    
    LINE_HEIGHT),                                                                                                                         //height
                    
    ((SkillName)cnt).ToString() );
                    
            
    GUI.Label(new Rect(OFFSET 4STAT_LABEL_WIDTH BASE_VALUELABEL_WIDTH BUTTON_WIDTH STAT_LABEL_WIDTH,     //x
                    
    statStartingPos + (cnt LINE_HEIGHT),                                                                                                                         //y
                    
    BASE_VALUELABEL_WIDTH,                                                                                                                                         //width
                    
    LINE_HEIGHT),                                                                                                                                                         //height
                    
    _toon.GetSkill(cnt).AdjustedBaseValue.ToString());
                }
            }
            private 
    void DisplayPointsLeft() {
            
    GUI.Label(new Rect(2501010025), "Points Left: " pointsLeft.ToString());
                    }
                    private 
    void DisplayCreateButton() {
                if (
    GUI.Button(new Rect(    Screen.width/50statStartingPos + (10 LINE_HEIGHT),100LINE_HEIGHT),"Create")) {
                    
    GameObject gs GameObject.Find("__GameSettings");
                    
    GameSettings gsScript GameObject.Find("__GameSettings").GetComponent<GameSettings>();
                    
                    
    gs.GetComponent<GameSettings>();
                    
                    
    gsScript.SaveCharacterData();
                    
                    
                    
    Application.LoadLevel("Level1");
                    }
                }
            }
     
    Last edited: Feb 8, 2011
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Could you post (or point out) just the line of code that's generating the error?
     
  3. Regless

    Regless

    Joined:
    Feb 8, 2011
    Posts:
    17
    Oh right, sorry.

    ;
     
  4. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Are you sure there's an active game object in the scene with that exact name?
     
  5. Regless

    Regless

    Joined:
    Feb 8, 2011
    Posts:
    17
    poistive. I've already started from scratch again so if I figure it out I'll post the solution in case someone else has the same problem.
     
  6. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Actually, are you sure that's the exact line that's generating the error? (I ask because that line shouldn't generate an exception regardless of whether the game object in question can be found.)
     
  7. Regless

    Regless

    Joined:
    Feb 8, 2011
    Posts:
    17
    *slams head on desk* sorry it was one right below it.

     
  8. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Are you sure the '__GameSettings' object has a GameSettings component?

    One thing you can do is break the statement up into two statements, one that acquires the game object and one that acquires the component. Then, use Debug.Log() statements or the debugger to determine which of those (if either) is null.
     
  9. Regless

    Regless

    Joined:
    Feb 8, 2011
    Posts:
    17
    Yep I sure the component is there. And I've broken the statement up several times thanks to your advice and was able to pin it down, unfortunately I still couldn't figure out the solution. I believe I am going to scrap this and start over, however I thank you for your patients and assistance. Cheers.
     
  10. geetoo

    geetoo

    Joined:
    Apr 21, 2009
    Posts:
    42
    Just a quick thought about it, but have you check the compilation order is OK ? Is your "GameSettings" script in the same folder (or a previously compiled folder) as your script having the problem.
    Check this page for info : http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html

    I'm not sure this would throw a NullReferenceError, but I once had problems with the script compilation priority thing, so I was thinking maybe it could help you. I hope it does.
     
    Last edited: Feb 9, 2011
  11. Regless

    Regless

    Joined:
    Feb 8, 2011
    Posts:
    17
    Well that's an interesting concept. Thanks for the link I'm going over it now. Hopefully it'ss mention something about why that error is bring up my E drive... It's an empty drive.