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 /
This question was closed Jan 21, 2018 at 11:13 PM by expat1999 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by expat1999 · Jan 17, 2017 at 06:15 AM · liststagswrapping

How would I get data out of a list of lists? (Made from a "List Wrapper")

Currently I am trying to basically create a "tag system" in which one object could take two objects which both have the script "Object Tag" and send their names to a script that would decide whether the first object could be built on top of the second object. (Think like "Indoors vs Outdoors" from Prison Architect for example)

Here's the code for a "String List Wrapper": using System.Collections; using System.Collections.Generic; using UnityEngine;

 [System.Serializable]
 public class StringListWrapper
 {
     public string tagName;
     public List<string> tagList;
 }

The "tagName" refers to the tag of the structure the player is trying to build, wheras the "tagList" refers to the list of other objects that the player is allowed to build that structure on top of.

Here's the code for the "Tag Checker": using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Tags : MonoBehaviour {
 
     public GameObject object1;
     public GameObject object2;
 
     public List<StringListWrapper> tags = new List<StringListWrapper>();
 
     private void Start()
     {
         TagCheck(object1, object2);
     }
 
     public void TagCheck(GameObject gO1, GameObject gO2)
     {
         List<string> TagSet1 = gO1.GetComponent<ObjectTag>().tags;
         List<string> TagSet2 = gO2.GetComponent<ObjectTag>().tags;
 
         bool check = false;
 
         for (int i = 0; i < TagSet1.Count; i++)
         {
             Debug.Log(tags[i]);
         }
 
     }
 }

What I am hoping to do is find an item in the "tags" list from the "Tags" script using a string name. But what I have found is that each item in the "tags" list is simply a "StringListWrapper". (As you can see I did a debug of the "tags" index.

If you need anymore clarification just ask! Also, if you have a suggestion for a better way of reaching my eventual goal please, tell me.

Thank you, --- expat1999

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

  • Sort: 
avatar image
1
Best Answer

Answer by Azrapse · Jan 17, 2017 at 06:41 AM

If I understood right, you want this:

  • Every structure describes itself with a "tag".

  • Every structure describes on top of which other structures it can be built with a collection of tags.

If you want this functionality in a Tags script, you could make it like this:

 using System.Collections.Generic;

 public class Tags: MonoBehaviour
     {
         public string tag;
         public HashSet<string> canBeBuiltOnTopOf = new HashSet<string>();
     
         public static bool CanBeBuiltOnTopOf(GameObject g1, GameObject g2)
         {
             var tags1 = g1.GetComponent<Tags>();
             var tags2 = g2.GetComponent<Tags>();
             if(tags1 == null || tags2 == null)
             {
                 return false;
             }
     
             return tags1.canBeBuiltOnTopOf.Contains(tags2.tag);
         }
     }

Then you could use it like this:

 bool allowed = Tags.CanBeBuiltOnTopOf(gameobject1, gameobject2);
Comment
Add comment · Show 4 · 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 expat1999 · Jan 17, 2017 at 06:52 AM 0
Share

Thank you for you're answer! :D

10/10 would stop being lazy and actually learn about HashSets/Tables again...

--- expat1999

avatar image expat1999 · Jan 18, 2017 at 12:43 AM 0
Share

Question: Can a hashset be shown in the inspector?

avatar image steo expat1999 · Jan 18, 2017 at 03:16 AM 0
Share

No, you can use only List from namespace System.Collections.Generics. HashSet, Dictionary, Queue are not shown.

avatar image expat1999 · Jan 18, 2017 at 05:57 AM 0
Share

Figured out how to make use of the code sample above. It's definitely the winner.

avatar image
1

Answer by bburtson09 · Jan 17, 2017 at 08:53 AM

In your loop you are iterating through your current class member tags List of type StringListWrapper

In your Loop if you change

Debug.Log (tags [i])

To:

Debug.Log (TagSet1[i])

It should return a string...

If you want to return the String field of your type StringListWrapper foreach item in your tag set You'll need to change debug log to:

Debug.Log (tags[i].tagName)

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 expat1999 · Jan 17, 2017 at 06:59 PM 0
Share

In hindsight I should have tried that already... Lol...

I'll test it out as soon as I can.

UPDATE: It's seems to work... still tinkering...

--- expat1999

avatar image expat1999 · Jan 18, 2017 at 06:00 AM 0
Share

O$$anonymous$$ messed around with this a bit. It got rather messy, so I'm going with the Hashsets above, but it was good to learn about the idea of "wrapping".

Follow this Question

Answers Answers and Comments

62 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

Related Questions

How to create a list from Children with different variables 1 Answer

Convert tags to list, then back to array 1 Answer

Using similar to GameObject.FindObjectsWithTag but for tag.contains. 0 Answers

How to not have a function affect a prefab? 2 Answers

Find GrandChildren of Grandparent Gameobject by tag. 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