• 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 /
  • Help Room /
avatar image
0
Question by drutt7 · Dec 28, 2016 at 05:10 AM · unity 5arrayliststringcollections

Building tycoon game, need help storing variables into an array.

Hi guys, sorry if this has been asked I've been googling it the last few days and haven't exactly been able to find anything useful to help.

So, I'm building a tycoon game, still new to unity but progressing well but I've hit a road block.

Basically i need an array that i can constantly add strings to the last index of the array(etc so there's something at index 0 i want to add the next item to 1, then 2 etc) I do not know how big the array is. i can't quite figure this one out.

I tried using a list but got some weird results such as it keeping data from the last run in the editor. Example, i run the game in the editor input two strings to the list "test1", "test2" i stop the game and run it again in the editor and i add another "TEST" variable and it prints "test1", "test2" and "TEST".

If anybody has a better idea for storing a lot of string variables other then a list or array i would be thankful.

TLDR; I need something i can add strings into without knowing how many strings will be put into it.

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

1 Reply

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

Answer by Socapex · Dec 28, 2016 at 10:26 PM

You want a List (which is a common container often called vector).

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Assertions;
 
 public class blee : MonoBehaviour
 {
     [NonSerialized] List<string> my_strings = new List<string>();
 
     void Start()
     {
         my_strings.Add("flaaflee");
         my_strings.Add("bleeblou");
         my_strings.Add("cheers m8");
 
         foreach (string x in my_strings) {
             Debug.Log(x);
         }
     }
 
 }


I just read the part about your serialization issues. If you make the list public, or use [SerializeField], data will be saved. This happens with most types you can use in unity. I added the [NonSerialized] attribute for you. This will prevent unity from saving the data.

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 drutt7 · Dec 29, 2016 at 07:50 AM 0
Share

Awesome, thank you so much :)

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

135 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

Related Questions

can't save values from another class to a list 1 Answer

All GameObjects list to a GameObject? 0 Answers

How do check which enum value was selected? 2 Answers

How to create new line in string from inspector? 1 Answer

save one big json string vs multi seperate json strings in playerprefs 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges