Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 jgiroux · Jul 20, 2015 at 04:00 PM · arrayslists

Possible to add array values within a to a script at runtime?

Hello, new coder here, been self learning for about a year now, and have never had to ask a question before, as I was always quite savvy enough to search for examples and find my own answers, however with this one, I am really stuck, cause I cant get my head around the logic (if its even possible)

BACKGROUND: working on an achievement system that has meta achieves that are dependent upon other normal achieves being completed first. I have a few variables. 1) string Title 2) string Description 3) string Category 4) int Points 5) int IconIndex

plus a dependency value that is set to null if there is none. however this is where I am running into a problem.

I know I can get the count of my dependency array , so I know how many dependent string values I have, but how can I actually loop through them and add them to code..

see examples:

My List function: ///////////////////////////////////////////////////////////////////////////////////////

  public List<AchievementInfo> AchievementLists = new List<AchievementInfo>();
         [System.Serializable]
                 public class AchievementInfo{
             public string Title = "Title";
             public string Description = "Enter a Description";
             public string Category = "General or Other";
             public int Points;
             public int SpriteIndexed;
             public string[] dependencies;
         }
 

////////////////////////////////////////////////////////////////////////////////////

the code I need to trigger:

CreateAchievment (STRING CATEGORY,STRING TITLE,STRING DESCRIPTION,INT ICONINDEX,STRING DEPENDENCIES (if NULL THEN VALUE = 0 *however if not NULL then see below for example);

CreateAchievment (STRING CATEGORY,STRING TITLE,STRING DESCRIPTION,INT ICONINDEX,new string[]{STRING TITLE(1) NEEDED, STRING TITLE(2) NEEDED,etc);

so you can see that this achieve may need one or two or twenty other achieves (TITLES) in order to trigger the earning of that meta achieve.

now in my code on start up I have: /////////////////////////////////////////////////////////

 void Start () 
     {
         for(int cnt = 0; cnt < AchievementLists.Count; cnt++) 
         {
             int icount = AchievementLists[cnt].dependencies.Length; 
             if (icount > 0)
             {
                 foreach (string x in AchievementLists[cnt].dependencies)
                 {
                     Debug.Log ("dependents: "+ x);
                 }
                 Debug.Log("DEENTitle: " + AchievementLists[cnt].Title + "  Decription:" + AchievementLists[cnt].Description + " Category: " + AchievementLists[cnt].Category + "Points: " +AchievementLists[cnt].Points.ToString() + "SpriteIndex: "+ AchievementLists[cnt].SpriteIndexed.ToString());
 
             }
 
             else{
                 CreateAchievment (AchievementLists[cnt].Category,AchievementLists[cnt].Title,AchievementLists[cnt].Description,AchievementLists[cnt].Points,AchievementLists[cnt].SpriteIndexed);
            //Debug.Log("Title: " + AchievementLists[cnt].Title + "  Decription:" + AchievementLists[cnt].Description + " Category: " + AchievementLists[cnt].Category + "Points: " +AchievementLists[cnt].Points.ToString() + "SpriteIndex: "+ AchievementLists[cnt].SpriteIndexed.ToString());
             }
             }

now this works fine if my array for dependencies is set to none (0), however how can I possibly plug in the number of dependencies into that same script line above?

I thought I could limit the amount of achieves needed for a meta achieve so I could do

if (List.count == 1) { CreateAchievment (STRING CATEGORY,STRING TITLE,STRING DESCRIPTION,INT ICONINDEX,STRING DEPENDENCIES#1) }

if (List.count == 2) { CreateAchievment (STRING CATEGORY,STRING TITLE,STRING DESCRIPTION,INT ICONINDEX,STRING DEPENDENCIES#1,STRING DEPENDENCIES#2) }

if (List.count == 3) { CreateAchievment (STRING CATEGORY,STRING TITLE,STRING DESCRIPTION,INT ICONINDEX,STRING DEPENDENCIES#1,STRING DEPENDENCIES#2,STRING DEPENDENCIES#3) }

etc etc... which right now seems to me the only thing that could work...

however, I was hoping of a more elegant solution without having to limit the amount of meta achieves we may want to have in our game design.

I hope I made myself clear enough... I am not used to asking questions of this depth.

also note the above code isn't real/compiled, its just there as a visual representation of what I am hoping to achieve.

Comment
Add comment · Show 1
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 Qo2770 · Jul 20, 2015 at 08:27 PM 0
Share

$$anonymous$$aybe try using a foreach()

 foreach(String Title in dependencies) {
      //doSomething
      //Title is the current element in dependencies, 
      //and it will loop through every String in dependencies
 }

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to return index of List element? 1 Answer

Help Sorting large class full of variables.,Creating and array of arrays of arrays of variables with different data types. 1 Answer

How do I create and loop through a completely generic list in javascript? 1 Answer

How to add to list if not in list 3 Answers

How to handle local arrays in unet? 0 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