• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-2
Question by ekahacksign_ · Nov 04, 2013 at 12:34 PM · nullreferenceexception

Null Reference Exception

when i want creating i got Nullalt text

 using UnityEngine;
 using System.Collections;
 using System;                //used for the Enum Class
 
 public class ChacaterGenerator : MonoBehaviour {
     private PlayerCharacter _toon;
     private const int STARTING_POINTS = 350;
     private const int MIN_STARTING_ATTRIBUTE_VALUE = 10;
     private const int STARTING_VALUE = 50;
     private int poinLeft;
     
     private const int OFFSET = 5;
     private const int LINE_HEIGHT = 20;
     
     private const int STAT_LABEL_WIDTH = 100;
     private const int BASEVALUE_LABEL_WIDTH = 30;
     private const int BUTTON_WIDTH = 20;
     private const int BUTTON_HEIGHT = 20;
     
     private int statStartingPos = 40;
     
     public GUIStyle myStyle;
     
     public GameObject playerPrefabs;
     
     // Use this for initialization
     void Start () {
         GameObject pc = Instantiate(playerPrefabs, Vector3.zero, Quaternion.identity) as GameObject;
         
         pc.name = "pc";
         
         _toon = pc.GetComponent<PlayerCharacter>();
 
         
         poinLeft = STARTING_POINTS;
         
         for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributName)).Length; cnt++){
             _toon.GetPrimaryAttribute(cnt).BaseValue = STARTING_VALUE;
             poinLeft -= (STARTING_VALUE - MIN_STARTING_ATTRIBUTE_VALUE);
         }
         
         _toon.StatUpdate();        
     }
     
     void Update () {        
     }
     
     void OnGUI() {
         DisplayName();
         DisplayPointsleft();
         DisplayAttributes();
         DisplayVitals();
         DisplaySkills();
         
         if(_toon.Name == "" || poinLeft > 0 )
             DisplayCreateLabel();
         else
             DisplayCreateButton();
         
     }
     
     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(AttributName)).Length; cnt++){
             GUI.Label(new Rect( OFFSET,
                                 statStartingPos + (cnt * LINE_HEIGHT),
                                 STAT_LABEL_WIDTH, 
                                 LINE_HEIGHT
                     ), ((AttributName)cnt).ToString());
             
             GUI.Label(new Rect( STAT_LABEL_WIDTH + OFFSET, 
                                 statStartingPos + (cnt * LINE_HEIGHT), 
                                 BASEVALUE_LABEL_WIDTH, 
                                 LINE_HEIGHT
                     ), _toon.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString());
             
             if(GUI.Button(new Rect( OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH, 
                                     statStartingPos + (cnt * BUTTON_HEIGHT), 
                                     BUTTON_WIDTH, 
                                     BUTTON_HEIGHT
                         ), "-")){
                 if(_toon.GetPrimaryAttribute(cnt).BaseValue > MIN_STARTING_ATTRIBUTE_VALUE){
                     _toon.GetPrimaryAttribute(cnt).BaseValue--;
                     poinLeft++;
                     _toon.StatUpdate();
                 }
             }
             if(GUI.Button(new Rect( OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH, 
                                     statStartingPos + (cnt * BUTTON_HEIGHT), 
                                     BUTTON_WIDTH, 
                                     BUTTON_HEIGHT
                             ), "+")){
                 
                 if(poinLeft > 0) {
                     _toon.GetPrimaryAttribute(cnt).BaseValue++;
                     poinLeft--;
                     _toon.StatUpdate();
                 }
             }
         }
     }
     
     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());
         }
     }
     
     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), 
                                 STAT_LABEL_WIDTH, 
                                 LINE_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), 
                                 BASEVALUE_LABEL_WIDTH, 
                                 LINE_HEIGHT
                                 ), _toon.GetSkill(cnt).AdjustedBaseValue.ToString());
         }
     }
     
     private void DisplayPointsleft() {
         GUI.Label(new Rect(250, 10, 100, 25), "Points Left: " + poinLeft.ToString());
     }
     private void DisplayCreateLabel() {
         GUI.Label(new Rect( Screen.width/ 2 - 50, statStartingPos + (10 * LINE_HEIGHT), 100, LINE_HEIGHT), "Creating...", "Button");
     }
 
     
     private void DisplayCreateButton() {
         if(GUI.Button(new Rect( Screen.width/ 2 - 50, statStartingPos + (10 * LINE_HEIGHT), 100, LINE_HEIGHT), "Create")) {
             GameSettings gsScript = GameObject.Find("_ _GameSettings").GetComponent<GameSettings>();
                 
             //change the cur value of the vitals to the max modified value of that vital
             UpdateCurVitalValues();
             
             //save the character data
             gsScript.SaveCharacterData();
             
             Application.LoadLevel(1);
         }
     }    
     private void UpdateCurVitalValues(){
         for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++){
             _toon.GetVital(cnt).CurValue = _toon.GetVital(cnt).AdjustedBaseValue;
         }
     }
 }
 
false 3.jpg (111.2 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mattssonon · Nov 04, 2013 at 12:46 PM 0
Share

Does the object have a GameSettings component?

avatar image ekahacksign_ · Nov 04, 2013 at 01:44 PM 0
Share

yes, im input GameSettings component using UnityEngine; using System.Collections; using System;

 public class GameSettings : $$anonymous$$onoBehaviour {
     
     void Awake() {
         DontDestroyOnLoad(this);
     }
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     public void SaveCharacterData(){
         GameObject pc = GameObject.Find("pc");
         
         PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter>();
         
         PlayerPrefs.DeleteAll();
         
         PlayerPrefs.SetString("Player Name", pcClass.Name);
         
         for(int cnt = 0; cnt < System.Enum.GetValues(typeof(VitalName)).Length; cnt++){
             PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Base Value", pcClass.GetPrimaryAttribute(cnt).BaseValue);
             PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Exp To Level", pcClass.GetPrimaryAttribute(cnt).ExpToLevel);
             
         
         }
             for(int cnt = 0; cnt < System.Enum.GetValues(typeof(VitalName)).Length; cnt++){
             PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Base Value", pcClass.GetVital(cnt).BaseValue);
             PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Exp To Level", pcClass.GetVital(cnt).ExpToLevel);
             PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Cur Value", pcClass.GetVital(cnt).CurValue);
         
             PlayerPrefs.SetString(((VitalName)cnt).ToString() + " - $$anonymous$$ods", pcClass.GetVital(cnt).Get$$anonymous$$odifyingAttrituesString());            
         }
             for(int cnt = 0; cnt < System.Enum.GetValues(typeof(SkillName)).Length; cnt++){
             PlayerPrefs.SetInt(((SkillName)cnt).ToString() + " - Base Value", pcClass.GetSkill(cnt).BaseValue);
             PlayerPrefs.SetInt(((SkillName)cnt).ToString() + " - Exp To Level", pcClass.GetSkill(cnt).ExpToLevel);
         
             PlayerPrefs.SetString(((SkillName)cnt).ToString() + " - $$anonymous$$ods", pcClass.GetSkill(cnt).Get$$anonymous$$odifyingAttrituesString());            
         }
     }
     
     public void LoadCharacterData() {
     
     }
     
 }
 
avatar image ekahacksign_ · Nov 04, 2013 at 01:46 PM 0
Share

this is Game $$anonymous$$aster

 using UnityEngine;
 using System.Collections;
 
 public class Game$$anonymous$$aster : $$anonymous$$onoBehaviour {
     public GameObject playerCharacter;
     public Camera mainCamera;
     
     public float zOffset;
     public float yOffset;
     public float xRotOffset;
     
     private GameObject _pc;
     private PlayerCharacter _pcScript;
 
     // Use this for initialization
     void Start () {
         _pc = Instantiate(playerCharacter, Vector3.zero, Quaternion.identity) as GameObject;
         
         _pcScript = _pc.GetComponent<PlayerCharacter>();
     
         zOffset = -2.5f;
         yOffset = 2.5f;
         xRotOffset = 22.5f;
         
         mainCamera.transform.position = new Vector3(_pc.transform.position.x, _pc.transform.position.y + yOffset, _pc.transform.position.z + zOffset);
         mainCamera.transform.Rotate(xRotOffset, 0, 0);
         
     }
     
     public void LoadCharacter() {
             GameSettings gsScript = GameObject.Find("_ _GameSettings").GetComponent<GameSettings>();
                 
             //loading the character data
             gsScript.LoadCharacterDatas();
 
     }
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jamster · Nov 04, 2013 at 06:18 PM

Short answer is that it either can't find the "_ _GameSettings" object or the object doesn't have a GameSettings script on it.

Try changing:

      GameSettings gsScript = GameObject.Find("_ _GameSettings").GetComponent<GameSettings>();

to:

          GameObject gsObject = GameObject.Find("_ _GameSettings");
          if( !gsObject ){
              Debug.LogError("No Object called _ _GameSettings!");
              return;
          }

          GameSettings gsScript = gsObject.GetComponent<GameSettings>();
          if( !gsScript ){
              Debug.LogError("No GameSettings script on _ _GameSettings object!");
              return;
          }

And that should give you a bit of info on where your problem lies.

If your problem is that there is not _ GameSettings object try changing the name of the _GameSettings object to just _GameSettings in case unity doesn't like it.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ekahacksign_ · Nov 04, 2013 at 06:57 PM 0
Share

im try change with you script, but i got message like this

No Object called _ _GameSettings! UnityEngine.Debug:LogError(Object) ChacaterGenerator:DisplayCreateButton() (at Assets/Scripts/Character Classes/ChacaterGenerator.cs:150) ChacaterGenerator:OnGUI() (at Assets/Scripts/Character Classes/ChacaterGenerator.cs:58)

avatar image ekahacksign_ · Nov 04, 2013 at 07:02 PM 0
Share

i change

 GameObject gsObject = GameObject.Find("_ _GameSettings");

to

 GameObject gsObject = GameObject.Find("_GameSettings");
 

i got same message No Object called _ _GameSettings! UnityEngine.Debug:LogError(Object) ChacaterGenerator:DisplayCreateButton() (at Assets/Scripts/Character Classes/ChacaterGenerator.cs:150) ChacaterGenerator:OnGUI() (at Assets/Scripts/Character Classes/ChacaterGenerator.cs:58)

avatar image Jamster · Nov 04, 2013 at 11:05 PM 0
Share

In that case double check that:

  • Your GameObject exists The name of

  • The GameObject is correct The name in

  • Your script is correct

And it should work :)

If all else fails make a variable and put the gameobject in that! :P

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Return value inexplicably becomes null 1 Answer

Why am I getting a NullReferenceException while trying to set a Sprite? (C#) 2 Answers

Why am i getting a NullReferenceException when i switch platform to Android? 0 Answers

Why am I getting this Null Reference Exception in the new Unity update. 1 Answer

Checking for null against Texture2D 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges