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 xyHeat · Jul 30, 2016 at 03:48 PM · c#listsstrings

Can we use a string instead a List<> name ? [C#]

Hey everybody ! I'm making a RPG and i need to check if all guards groups in a city are full life. To make that, i use a List who contain 5 list of Guards (PNJ). So, my question is : Can i do something like this :

     [Header("Guards lists")]
     public List<List<PNJ>> listGroupOfGuard;
     public List<PNJ> baseGroupOfGuards1;
     public List<PNJ> baseGroupOfGuards2;
     public List<PNJ> baseGroupOfGuards3;
     public List<PNJ> baseGroupOfGuards4;
     public List<PNJ> baseGroupOfGuards5;
     public List<PNJ> groupOfGuards1;
     public List<PNJ> groupOfGuards2;
     public List<PNJ> groupOfGuards3;
     public List<PNJ> groupOfGuards4;
     public List<PNJ> groupOfGuards5;
 
 
   public void RespawnGuards()
     {
         string j = "baseGroupOfGuards";
         string k = "groupOfGuards";
         string l = "";
         string m = "";
         for (int i = 1; i < 6; i++)
         {
             l = j + i; // l = "baseGoupOfGuards" + i (this is the prefabs)  (guards can't be killed)
             m = k + i; // m = "groupOfGuards" + i   (this is not the prefabs) (guards can be killed)
             foreach (List<PNJ> listPNJ in listGroupOfGuard)
             {
                 if((List<PNJ>)m != (List<PNJ>)l)
                 {
                     (List<PNJ>)m = (List<PNJ>)l;
                 }
             }
         }
     }
 


Or i must make something for each groups ?

Thank you !

Bye, xyHeat

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
2
Best Answer

Answer by ScaniX · Jul 30, 2016 at 04:44 PM

Well, you cannot cast a string to a list. I am not sure what you want to achieve, so I will just take a guess. :)

 public List<List<PNJ>> listBaseGroupOfGuard = new List<List<PNJ>>();
 public List<PNJ> baseGroupOfGuards1 = new List<PNJ>();
 public List<PNJ> baseGroupOfGuards2 = new List<PNJ>();
 public List<PNJ> baseGroupOfGuards3 = new List<PNJ>();
 public List<PNJ> baseGroupOfGuards4 = new List<PNJ>();
 public List<PNJ> baseGroupOfGuards5 = new List<PNJ>();

 public List<List<PNJ>> listGroupOfGuard = new List<List<PNJ>>();
 public List<PNJ> groupOfGuards1 = new List<PNJ>();
 public List<PNJ> groupOfGuards2 = new List<PNJ>();
 public List<PNJ> groupOfGuards3 = new List<PNJ>();
 public List<PNJ> groupOfGuards4 = new List<PNJ>();
 public List<PNJ> groupOfGuards5 = new List<PNJ>();
 
 void Start() {
     listGroupOfGuard.Add(groupOfGuards1);
     listGroupOfGuard.Add(groupOfGuards2);
     listGroupOfGuard.Add(groupOfGuards3);
     listGroupOfGuard.Add(groupOfGuards4);
     listGroupOfGuard.Add(groupOfGuards5);
     listBaseGroupOfGuard.Add(baseGroupOfGuards1);
     listBaseGroupOfGuard.Add(baseGroupOfGuards2);
     listBaseGroupOfGuard.Add(baseGroupOfGuards3);
     listBaseGroupOfGuard.Add(baseGroupOfGuards4);
     listBaseGroupOfGuard.Add(baseGroupOfGuards5);
 }

Now unless you are reassigning the members baseGroupOfGuards? and groupOfGuards?, you can do this to get a reference to those lists in a loop (without using reflections):

 public void RespawnGuards()
  {
      List<PNJ> l;
      List<PNJ> m;
      for (int i = 1; i < 6; i++)
      {
          l = listBaseGroupOfGuard[i-1]; // == baseGroupOfGuardsX, where X == i
          m = listGroupOfGuard[i-1]; // == groupOfGuardsX, where X == i
          // Do something with those groups like refill m by instantiating something from l
          // Sorry, I wasn't able to guess what your loop body was supposed to do. :)
      }
  }



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 xyHeat · Jul 30, 2016 at 06:34 PM 0
Share

Thank you for your answer !

" // Do something with those groups like refill m by instantiating something from l // Sorry, I wasn't able to guess what your loop body was supposed to do. :) "

This loop will refill groups which aren't full like you said :)

Thank you again !

Bye, xyHeat

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

210 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 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

Making lists out of other lists 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How Do I Convert String to String Array In List.Add? 1 Answer

Is it possible to choose from preinitialized lists in the inspector? 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