• 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 george3d2011 · Jun 14, 2014 at 12:49 PM · texturearrayreferencenullexception

Null Reference Exception After refering to an Array

Hey guys im making a zombie survival game and I am facing a problem with levels. I have basicly made a menu where you can edit your player and in this menu I want my player's level to be appeared.. the problem is that i have set up the level up logic in a different script so, i am refering to it from my main menu script (as my player edit screen is in my main menu) my code is this:

 public Texture[] levelTextures;
 public string playerName;
 
 public void OnGUI()
 {
 GameObject manager = gameObject;
 PlayerController pCont = manager.GetComponent<PlayerController>();
 
 GUI.Label(new Rect(Screen.width / 2 + 115, Screen.height / 2-25, 100,100), "Player Level", mainMenuLetters);
 
             GUI.Label(new Rect(Screen.width / 2 + 50, Screen.height / 2 + 35, 100,100), "[" + playerClan + "]" + playerName + ": " + pCont.playerLevel, mainMenuLetters);
             GUI.DrawTexture(new Rect(Screen.width / 2 + 100, Screen.height / 2 + 35, 100,100), levelTextures[pCont.playerLevel - 1]);
 }

Note***: playerLevel is an int variable.

My Error is this: NullReferenceException: Object reference not set to an instance of an object MultiplayerScript.MainMenu () (at Assets/Scripts/MultiplayerScript.cs:475)

Note2***: line 475 = GUI.Label(new Rect(Screen.width / 2 + 50, Screen.height / 2 + 35, 100,100), "[" + playerClan + "]" + playerName + ": " + pCont.playerLevel, mainMenuLetters);

I really can't solve this problem on my own i have tried everything please help me.

Comment
Add comment · Show 2
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 Nerevar · Jun 14, 2014 at 06:46 PM 0
Share

is the main script attached to the game object having the PlayerController component?

because when you assign pCont you are actually doing

  pCont = this.GetComponent<PlayerController>();
avatar image george3d2011 · Jun 15, 2014 at 01:15 PM 0
Share

yeah man that was the issue.. i was refering to a script that didnt exist on that gameobject now i realised it :) thank you very much!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robusto · Jun 14, 2014 at 01:27 PM

in your inspector for this gameobject, look under this script/component and look at levelTextures array, if any of them say NONE, then they are null / not pointing to anything. Somewhere you have to load textures or set them equal to something.

Comment
Add comment · Show 1 · 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 george3d2011 · Jun 14, 2014 at 05:49 PM 0
Share

thank you man!! that was really helpful!

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

C# Null Object Reference - Driving me mad! 1 Answer

NullReferenceException on Photon Unity RPC Call 1 Answer

Exception: not implemented? 2 Answers

NullReferenceException Object reference error 1 Answer

NullRefernceExcepception with Raycasting 1 Answer


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