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 1jstoffer · Jan 17, 2018 at 01:34 AM · arraysdeclarationclear

Declare and clear an Array

I am attempting to clear an array prior to rebuilding it and am getting a declaration error I don't understand.

"Assets/Scripts/SetToppings.cs(42,27): error CS0844: A local variable pieces' cannot be used before it is declared. Consider renaming the local variable when it hides the member SetToppings.pieces'"

here is my code. Thanks!

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System;

public class SetToppings : MonoBehaviour {

 public Text Item;
 public Text price;
 public string[] pieces = new string[0];
 public string[] prices = new string[0];

 public void RemoveTopping(){

     Array.Clear (pieces, 0, pieces.Length);
     Array.Clear (prices, 0, pieces.Length);

. . .

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jan 17, 2018 at 02:24 AM

Ok first of all the error you quoted is about a piece of code you did not include in your question. However it seems you declared another local variable with the same name "pieces" inside a method. This results in a name collision.


Next thing is arrays have a fix size that can't be changed. You created two empty string arrays. So they can't hold a single element. Again arrays can't be resized. You actually have to create a new array with a larger / smaller size and copy the old elements you want to keep over.


Next Array.Clear is just a helper method which iterates through all elements of your array and sets each element to the default value of the element type. The default value for string is null. This does not "remove" the elements.


If you need a dynamic collection of things you better use a generic List. A List internally uses an actual array to hold the elements but it tries to minimize the reallocation of the internal array. A List has an Add and Remove method. It allows the same random access as an array does.

 public List<string> pieces = new List<string>();

Initially the List is empty. You can add elements by using:

 pieces.Add("Some string");

You can also clear the whole list by using

 pieces.Clear();

Now the list is empty again. You can check how many elements are in the List by using the Count property:

 for(int i = 0; i < pieces.Count; i++)
 {
     Debug.Log("Element #"+i+" == " + pieces[i]);
 }
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 Bunny83 · Jan 17, 2018 at 02:28 AM 0
Share

ps: You should learn to read error messages:

 "Assets/Scripts/SetToppings.cs(42,27):
                                |   |
       Line number _____________/   |
       Column      _________________/

avatar image Ginxx009 Bunny83 · Jan 17, 2018 at 02:29 AM 0
Share

Damn so fast @Bunny83 . Hahaha

avatar image
0

Answer by Ginxx009 · Jan 17, 2018 at 02:28 AM

Wouldn't it be easier if you use list instead .

 public List<string> pieces = new List<string>
 
 //here is adding the value
 pieces.Add(SomeValue)
 
 //here is the clearing of value
 pieces.Clear();
 

I guess you're currently declaring new variables and this has the effect of hiding the class members. The compiler also thinks you're using those declared variables earlier in the function which is causing the error.

Hope it helps

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

Answer by 1jstoffer · Jan 17, 2018 at 03:16 AM

That does clarify some things. I was re-declaring the array later in the method. I am going to try redoing the code as a list. I just need to figure out how to use the Split function with lists since it normally returns an array.

     pieces = ToppingData.ToppingA1.Split ('/');
     prices = ToppingData.PriceA1.Split ('/');

I'll see what I can do and re-post any issues.

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 Bunny83 · Jan 17, 2018 at 04:54 AM 0
Share

You can initialize a List with anything "enumerable" (i.e. everything that implements the IEnumerable interface). So you can just do:

 pieces = new List<string>(ToppingData.ToppingA1.Split ('/'));

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

75 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

Related Questions

Arrays declaration c# 3 Answers

Declaring a type in JavaScript arrays 3 Answers

Declaring an Array as a combination of multiple other arrays? 1 Answer

XML File, or Array? 1 Answer

Instantiate random object from Array 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