• 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
Question by KReesLtd · Feb 27, 2020 at 03:04 PM · listbeginnerarraysstaticlists

Creating a list using a list from another script?

 public static List<int> LeaderPBA = new List<int>() { RankingData.UserRankScore[0], RankingData.JoshuaRankScore[0], RankingData.WhyteRankScore[0], RankingData.FuryRankScore[0] };

Thats how im calling the list and stating whats within it below is how im getting the lists from the other script.

 public static List<int> UserRankScore; //Score (Etc..........)

I dont see why this isnt working and i get this may be a long solution and a better fix but i am new to coding.

error it gives tells me to check "NullReferenceException" and the "object reference is not set to an instance of an object"

any help would be much appreciated

Comment

People who like this

0 Show 6
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 tormentoarmagedoom · Feb 27, 2020 at 03:07 PM 0
Share

Hello.

You post is almost correct, but still bad developed...

Where NullReferenceException? at an element of the list when trying to declare it? When trying to read your new list?... where..

avatar image KReesLtd tormentoarmagedoom · Mar 09, 2020 at 10:35 AM 0
Share

i've added screenshots to give better understanding of my problem below

avatar image KReesLtd KReesLtd · Mar 09, 2020 at 12:19 PM 0
Share

it runs the error when i try and define the new list btw

Show more comments
Show more comments
avatar image KReesLtd · Mar 04, 2020 at 11:37 AM 0
Share

I hope this makes it more clear i cant work out why this error occurs?

[1]: /storage/temp/153624-error2.jpg

error.jpg (324.3 kB)
error2.jpg (204.4 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Mar 09, 2020 at 02:46 PM

Well the issue is pretty simple. You try to create and fill your "LeaderPBA" list inside a static field initializer. This will be evaluated way before the first object in your scene will be created / loaded / initialized. Furthermore static field initializers will most likely be executed from Unity's loading thread because that will be the first time your class will be "touched". It should be pretty obvious that at this time your static "UserRankScore" variable does not contain any list yet, let alone any useable data.


You should avoid static variables whenever possible, especially when you seemingly have no idea what they actually do or how they work ^^. You should move your list creation to some method which will be called after your other lists have been created and contain meaningful data. Since we don't see anything else of your code this is where I will stop explaining.

Comment

People who like this

0 Show 0 · 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

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

137 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can you do calculations on two lists? 1 Answer

How do i convert this code snippet to a list? 1 Answer

Undo/back system using a List/Array 2 Answers

Reading all data in the list with duplicate data inside 1 Answer

How to return index of List element? 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