Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by acmeydan · Oct 10, 2015 at 10:10 PM · arraylistfloat

how to define spesific object in list?

Hi

Maybe its very noob question, or I dont know how to ask, please forgive my mistake;

In my "script1" there are list of resources. they are declared as

 public static float[] resources;

In my "script2" there is a line like this;

 for (int i = 0; i < lbl_resources.Length; i++)
         {
             lbl_resources[i].text = Script1.resources[i].ToString();
         }

I need to know spesific resources in the game, not resource[ ], because I will access them later. like resource1=100, resource2=200

As far as I know; I thought It should be like this;

 void Awake()
 {
 resource[1]=100; 
 resource[2]=50;
  (...)
 }

It doesnt give any error after I've edit on this script, but they will give both error on play mode;

NullReferenceException: Object reference not set to an instance of an object Script1.Awake ()

and

"NullReferenceException: Object reference not set to an instance of an object Script2.Update ()"

Where is my wrong? Can you help me?

Thank you very much for your patience.

Comment
Add comment
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

2 Replies

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

Answer by Bunny83 · Oct 10, 2015 at 11:17 PM

You just "declared" an array variable, but you never create an actual array. Arrays have a fix size which has to be specified when they are created.

So if you want to store 5 float values in that array do this:

 void Awake()
 {
     resource = new float[5]; // create a new float array with 5 elements
     resource[0] = 100f;
     resource[1] = 50f;
     resource[2] = 25f;
     resource[3] = 12.5f;
     resource[4] = 6.25f; 
     // The next line would cause an index out of bounds error since we only have 5 elements ( 0 - 4 )
     // resource[5] = 1f;
 }

You used a static array, Static variables are shared between all instances of that class. So you usually want to initialize it directly like this:

 public static float[] resources = new float[] {100f, 50f, 25f, 12.5f, 6.25f };

Here we create the array in the field initializer. As you can see when you provide the values like this you don't need to specify the element count. The compiler does this automatically. Since we have 5 values in there the array will automatically be of size 5.

Static variables can't be serialized by Unity since they don't belong to a concrete instance. If you declare an array like this in a MonoBehaviour class:

 public float[] resources;

you usually don't initialize the values manually. The Inspector of the editor will automatically create an instance (initially with 0 elements) and serialize those values. That allows you to edit the array in the Inspector.

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 acmeydan · Oct 11, 2015 at 05:50 PM 0
Share

Thank you very much. It works and I understood the mechanism.

avatar image
0

Answer by xyz567 · Oct 10, 2015 at 10:40 PM

This is not actually a list it is an array. Lists can be used by including using Systems.Collections.Generic at the top of a script and are declared as List<string> strings = new List<string>() which would make a new list of strings.

Your question refers to arrays. The reason you get this error is because in the first script you fill the 2nd and 3rd elements of the array but not the first (Remember computers start counting from 0 not 1) and in your second script you start the loop from 0 and the array does not have a value at array position 0 so it is just a null value.

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 acmeydan · Oct 11, 2015 at 05:41 PM 0
Share

Thank you for your answer, I thought like that after I post this question. Even I changed 1 to 0, situtation didnt change.

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

32 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

Related Questions

A node in a childnode? 1 Answer

Pick between two floats 2 Answers

Array or List holding 3 floats, multidimensional, Jagged? 1 Answer

Looping through Button Array and checking for OnClick 0 Answers

Sorting a list by distance to an object? 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