• 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
0
Question by Nynex71 · Jan 16, 2011 at 12:15 AM · nullreferenceexception

Confused about c# scripting

For some reason this script is messing me up

public static PlayerInformationController PFC;

void OnGUI() { GUI.Label(new Rect(10, 70, 100, 20), "Dollars = " + PFC.curMoney); }

I believe that all of the is correct and i get no errors but when i go into debug mode i get billions of these

NullReferenceException: Object reference not set to an instance of an object
MyGUI.CharacterSkillsGUI () (at Assets/ShiftedIrony Assets/Scripts/PlayerScripts/MyGUI.cs:143)
MyGUI.OnGUI () (at Assets/ShiftedIrony Assets/Scripts/PlayerScripts/MyGUI.cs:59)

Don't really know what is messing it up but atm im trying to fix. that small script is not the entire script its just the part the the problem is located in. i don't think i need it to be public static but i put it in there anyways either way i get that error.

EDIT:

PFC SCRIPT

using UnityEngine; using System.Collections;

public class PlayerInformationController : MonoBehaviour { public int curMoney = 0; public int curHealth = 100; public int maxHealth = 100;

 private string PlayerName = "Test";

 public GUIText CharacterChat;

 public Transform playerSpawn; 

 public static MyGUI mg;

 public float healthBarLength;

 void Start () { 

     healthBarLength = Screen.width;
 }

 // Update is called once per frame
 void Update () {        
     AdjustCurrentHealth(0);
     }
 void OnGUI() {
     GUI.Label(new Rect(10, 10, 100, 20), PlayerName);       

     GUI.Box(new Rect(10, 30, Screen.width / 16, 20), curHealth + "/" + maxHealth);
 }
     public void AdjustCurrentHealth(int adj) {
             curHealth +=adj;

         if(curHealth < 1)
             curHealth = 0;

         if(curHealth == 0)
             transform.position = playerSpawn.position;

         if(curHealth == 0)
             curHealth = maxHealth;

         if(curHealth > maxHealth)
             curHealth = maxHealth;

         if(maxHealth < 1)
             maxHealth = 1;
         }

}

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 Jesse Anders · Jan 16, 2011 at 12:22 AM 0
Share

Is PlayerInformationController a class or a struct? And have you assigned a value to the variable PFC anywhere?

avatar image Nynex71 · Jan 16, 2011 at 01:20 AM 0
Share

PlayerInformationController is a script and i was told that i could respond to other variables in different scripts by making another variable that looked something like this "public (script name) (how you call for it)" i don't know if that answers what you were asking but ill add the PlayerInformationController script to the post and you could figure it out im pretty sure its a class but im not exactly sure on how to tell...

avatar image Nynex71 · Jan 16, 2011 at 02:19 AM 0
Share

does no one know how to do this?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Peter G · Jan 16, 2011 at 02:39 AM

NullReference would mean that your PFC is null when Unity went to check a value in it. I don't see any place where you are assigning it a reference unless you are doing it in the inspector.

You should probably do something like:

private static PlayerInformationController pfc;

public static PlayerInformationController PFC { get { if(pfc == null) { pfc = FindObjectOfType(typeof(PlayerInformationController)) as PlayerInformationController; } return pfc; } }

void OnGUI() { GUI.Label(new Rect(10, 70, 100, 20), "Dollars = " + PFC.curMoney.ToString()); }

EDIT: And I noticed, PFC.curMoney is an int and label needs a string so I fixed it for you.

Comment
Add comment · Show 2 · 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 Nynex71 · Jan 16, 2011 at 04:05 AM 0
Share

what does this mean?

Assets/ShiftedIrony Assets/Scripts/EnemyScripts/EnemyAI.cs(82,15): error CS0200: Property or indexer `EnemyAI.$$anonymous$$G' cannot be assigned to (it is read only)

avatar image Peter G · Jan 16, 2011 at 04:35 AM 0
Share

It's hard to say without seeing some of your code. Did you make a setter in your property? Or define a variable with a readonly modifier?

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

No one has followed this question yet.

Related Questions

NullReferenceException: Object reference not set to an instance of an object 2 Answers

MoveTowards doesn't work after changing tag. 2 Answers

NullPointerException while changing a variable from one script in another 1 Answer

array initialisation c# 1 Answer

Unity the NullReferenceException wizard 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