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
0
Question by kilian277 · Apr 05, 2013 at 03:14 PM · arrayxmlfor-loop

Error use of unassigned local variable

Hi,

i need to fill a array with a for loop for each item in an xml file.

Within in the forloop by the first line it's says "use of unassigned local variable items" I tried everything and cannot find a solution.

Anyways here the code:

 public Item[] loadXmlFile(string filename,string tagname)
     {
         Item[] items;
         TextAsset file;
         file = Resources.Load(filename) as TextAsset;
         XmlDocument xmlFile = new XmlDocument();
         xmlFile.LoadXml(file.text);
         XmlNodeList listNodes = xmlFile.GetElementsByTagName(tagname);
 
         for (int i = 0; i <= listNodes.Count; i++)
         {
             items[i].itemName = listNodes[i].Attributes["itemname"].Value;
             items[i].prefab = (Transform)Resources.Load(listNodes[i].Attributes["prefab"].Value);
             items[i].icon = (Texture2D)Resources.Load(listNodes[i].Attributes["icon"].Value);
             items[i].buildCost = listNodes[i].Attributes["buildcost"].Value;
             items[i].maxInhab = listNodes[i].Attributes["maxInhab"].Value;
             items[i].elecConsump = listNodes[i].Attributes["elecconsump"].Value;
             items[i].waterConsump = listNodes[i].Attributes["waterconsump"].Value;
             items[i].xpAdd = listNodes[i].Attributes["xpadd"].Value;
             items[i].itemText = listNodes[i].Attributes["itemtext"].Value;
         }
         return items;
     }
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 1337GameDev · Apr 05, 2013 at 04:12 PM

You should initialize the array with a specified number of elements.

Use: Item items[] = new Item[sizeHere];

Then you can populate the array.

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 kilian277 · Apr 05, 2013 at 04:22 PM 0
Share

Yes , that gets rid of the unassigned error now it gives me a null reference exception again at the same line (line 12) ("Object reference not set to an instance of an object). I feel dumb today i really have no clue whats going on here ?

avatar image kilian277 · Apr 05, 2013 at 05:05 PM 0
Share

Never$$anonymous$$d i got it to work thanks!

avatar image
0

Answer by Aria-Lliane · Apr 05, 2013 at 06:44 PM

Maybe I'm being silly but just like the error says "items" is not initialized?

 //this will be before the for loop
 items = new item[listNodes.Count];
 

And by the way, since 'i' starts at 0 you dont go to "i<=listNodes.Count" but to "i< listNodes.Count" right?

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 kilian277 · Apr 05, 2013 at 07:07 PM 0
Share

Yes it comes before the for loop. if the array size is 5 and you use i=0 < then it will loop five times, if you use i=0

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

12 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

Related Questions

rotating vertices with for loop in 0 Answers

GUI button image next 2 Answers

C# Killfeed issues 0 Answers

Array error, cannot convert float to float[] ???? 2 Answers

Where should I use a traditional forloop as opposed to 'for (var i in monsters)'? 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