Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
-1
Question by ThaiThao · Dec 14, 2011 at 11:41 PM · c#nullreferenceexception

NullReferenceException script problem?

here is a video to help explain what i am talking about

Video

then here is the code for the first script that is used to call Gameset..

using UnityEngine; using System.Collections; using System; //used for the Enum class

public class CharGen: MonoBehaviour { private PlayerChar _toon; private const int STARTING_POINTS = 350; private const int MIN_STARTING_ATTRIBUTE_VALUE = 10; private const int START_VALUE = 50; private int pointsLeft;

 private const int OFFSET = 5;
 private const int LINE_HIEGHT = 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_HIEGHT = 20;
 
 private int statStartingPos = 40;
 
 
 public GUISkin mySkin;
 
 public GameObject playerPrefab;

 // Use this for initialization
 void Start () {
     GameObject pc = Instantiate(playerPrefab, Vector3.zero,    Quaternion.identity) as GameObject;
     
     pc.name = "playerchar";
     

// _toon = new PlayerChar(); // _toon.Awake();

     _toon = pc.GetComponent<PlayerChar>();

     
     pointsLeft = STARTING_POINTS;
     
 for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++)    {
     _toon.GetPrimaryAttribute(cnt).BaseValue = START_VALUE;    
     pointsLeft -= (START_VALUE - MIN_STARTING_ATTRIBUTE_VALUE);
 }
 
         _toon.StatUpdate();
 
 }
 
 // Update is called once per frame
 void Update () {

 
 }
 
 void OnGUI()    {
     GUI.skin = mySkin;
     DisplayName();
     DisplayPointsLeft();
     DisplayAttribute();
     DisplayVital();
     DisplaySkill();
     
     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 DisplayAttribute()    {
     for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++)    {
         GUI.Label(new Rect(    OFFSET,                                                     //z
                                         statStartingPos + (cnt * LINE_HIEGHT),         //y
                                         STAT_LABEL_WIDTH,                                 //width
                                         LINE_HIEGHT                                                //height
                                     ), ((AttributeName)cnt).ToString());
         
         GUI.Label(new Rect(    STAT_LABEL_WIDTH + OFFSET,                     //x
                                         statStartingPos + (cnt * LINE_HIEGHT),         //y
                                         BASEVALUE_LABEL_WIDTH,                         //width
                                         LINE_HIEGHT                                                //height
                                     ), _toon.GetPrimaryAttribute(cnt).AddjustedBaseValue.ToString());
         
         if(GUI.Button(new Rect(    OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH,    //x
                                                 statStartingPos + (cnt * BUTTON_HIEGHT),                             //y
                                                 BUTTON_WIDTH,                                                                     //width
                                                 BUTTON_HIEGHT                                                                    //height
                                         ),    "-"))    {
             if( _toon.GetPrimaryAttribute(cnt).BaseValue > MIN_STARTING_ATTRIBUTE_VALUE)    {
                 _toon.GetPrimaryAttribute(cnt).BaseValue--;
                 pointsLeft++;
                         _toon.StatUpdate();
         }
         }
         
         if(GUI.Button(new Rect(    OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH,    //x
                                                 statStartingPos + (cnt * BUTTON_HIEGHT ),                                                         //y
                                                 BUTTON_WIDTH,                                                                                                 //width
                                                 BUTTON_HIEGHT                                                                                                 //height
                                             ),    "+"))    {
             if(pointsLeft > 0)    {
                 _toon.GetPrimaryAttribute(cnt).BaseValue++;
                 pointsLeft--;
                         _toon.StatUpdate();
         }
     }
 }

}

 private void DisplayVital()    {
             for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++)    {
         GUI.Label(new Rect(OFFSET, statStartingPos + ((cnt + 8) * LINE_HIEGHT), STAT_LABEL_WIDTH, LINE_HIEGHT), ((VitalName)cnt).ToString());
         GUI.Label(new Rect(OFFSET + STAT_LABEL_WIDTH, statStartingPos + ((cnt + 8) * LINE_HIEGHT), BASEVALUE_LABEL_WIDTH, LINE_HIEGHT), _toon.GetVital(cnt).AddjustedBaseValue.ToString());
     }
 }
 
 private void DisplaySkill()    {
     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_HIEGHT), STAT_LABEL_WIDTH, LINE_HIEGHT), ((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_HIEGHT), BASEVALUE_LABEL_WIDTH, LINE_HIEGHT), _toon.GetSkill(cnt).AddjustedBaseValue.ToString());
     }
 }
 
 private void DisplayPointsLeft()    {
     GUI.Label(new Rect(250, 10, 100, 25),    "Points Left:"    + pointsLeft.ToString());
 }
 
 private void DisplayCreateButton()    {
     if(GUI.Button(new Rect(Screen.width/ 2 - 50, statStartingPos + (11 * LINE_HIEGHT), 100, LINE_HIEGHT),    "Create"))    {
         GameObject gs = GameObject.Find("__gamesetting");
         
         Gameset gsScript = gs.GetComponent<Gameset>();
         
         gsScript.SaveCharData();
         
         Application.LoadLevel("Souls of Blades");
     }
     
     
 }
 

}

then here is the script Gameset

using UnityEngine; using System.Collections; using System;

public class Gameset : MonoBehaviour {

 void awake()    {
     DontDestroyOnLoad(this);
 }

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }
 public void SaveCharData()    {
     GameObject pc = GameObject.Find("pc");
     
     PlayerChar pcClass = pc.GetComponent<PlayerChar>();
     
     PlayerPrefs.SetString("Player Name",    pcClass.Name);
     
     for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++)    {
         PlayerPrefs.SetInt(((AttributeName)cnt).ToString(),    pcClass.GetPrimaryAttribute(cnt).BaseValue);
     }
 }
 
 public void LoadCharData()    {
     
 }

}

Comment
Add comment · Show 1
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 syclamoth · Dec 14, 2011 at 11:52 PM 2
Share

Don't post a grainy video and expect us to know what you're talking about. You should actually take the time to write out and explain exactly what your problem is. $$anonymous$$aybe it will become clear to you while you do that what is going on!

Also, don't just post your entire codebase here- only post the bits that are actually relevant. We don't need to know that your classes inherit from $$anonymous$$onoBehaviour, and import UnityEngine. That's usually assumed to be the case, unless stated otherwise. So don't post it! Usually problems arise in just one or two lines, or interactions between a few lines- not the entire script.

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

I'm having more problems accessing a variable from another script in c# 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Im getting a "NullReferenceException: Object reference not set to an instance of an object" error not sure why. 1 Answer

C# Need Help Finding Where Null Reference Exception Is 2 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