Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Jun 28, 2017 at 08:48 AM by Trevdevs for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Trevdevs · Jun 28, 2017 at 07:41 AM · listlistsclassescontainsclass instance

How to search a certain variable in a list of a created class (C#)

Wasn't to sure how to word the question so here's a description with code below.

I have a list which takes type of Room (a class i created) and this class has a contructor to create many instances of itself with 2 parameters one being a enum called Size and another being a Vector3 called position. What i want to do is make sure when i create a new room its not being created on another at the same position, which i've done sucessfully with many switch statements which yes i know doens't look the prettiest but i'll clean it up later. However I need to check and see if the list "Contains" this position in the Room the problem is the List is of type Room and I need to check a part in it.

Since I cant do if(!rooms.Contains(room.position)) and can only do if(!rooms.Contains(room)) which i dont know if its checking that position variable and am getting a infinite loop, which is why the while loop is marked out. Sorry if the description is poor if you have any questions leave a comment. And here is the code

Room class

 public class Room {
 
     public Size roomSize;
     public Vector3 roomPos;
 
     public Room(Size zRoomSize, Vector3 zRoomPosition)
     {
         roomSize = zRoomSize;
         roomPos = zRoomPosition;
     }
 }

MapGenerator Class

-Part i'm having issues with

             Room newRoom = new Room (newSize, newPos);
 
 
             //What I want but can't do :(
             if(!rooms.Contains (newRoom.roomPos))
 
             //What i can do but doesn't work how i want because its checking the size also and I only want it to check the position 
             if(!rooms.Contains (new Room(newSize, newPos)))
             {
                 rooms.Insert (0, newRoom);
             }


-Whole code (Yes i'll clean it up later right now i just want it to work and i'm so close :) )

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MapGenerator : MonoBehaviour
 {
     public List<Room> rooms = new List<Room>();
 
     public enum Size {small, medium, large};
 
     public int maxRooms;
     public GameObject smallRoom;
     public GameObject mediumRoom;
     public GameObject largeRoom;
 
     void Start()
     {
         rooms.Add (new Room(Size.small, Vector3.zero));
         GenerateMap ();
     }
         
     void GenerateMap()
     {
         //while(maxRooms > rooms.Count)
         //{
             SetNextRoom (rooms[0], 3);
         //}
         for(int i = 0; i < rooms.Count; i++)
         {
             GameObject roomObj = null;
             switch (rooms[i].roomSize)
             {
                 case Size.small:
                     roomObj = Instantiate (smallRoom, rooms[i].roomPos, Quaternion.identity) as GameObject;
                     break;
 
                 case Size.medium:
                     roomObj = Instantiate (mediumRoom, rooms[i].roomPos, Quaternion.identity) as GameObject;
                     break;
 
                 case Size.large:
                     roomObj = Instantiate (largeRoom, rooms[i].roomPos, Quaternion.identity) as GameObject;
                     break;
             }
             roomObj.name = "Room Number: " + i + " " + "RoomSize: " + rooms[i].roomSize + " RoomPosition: " +  rooms[i].roomPos.ToString ();
             roomObj.transform.SetParent (this.transform);
         }
     }
 
     void SetNextRoom(Room previousRoom ,int maxPaths)
     {
         for (int i = 0; i < maxPaths; i++)
         {
             int randomIntDirection = Random.Range (0, 4);
             int randomIntSize = Random.Range (0, 3);
 
             int offset = 0;
             Vector3 newPos = previousRoom.roomPos;
             Size newSize = Size.small;
             switch(randomIntSize)
             {
                 case 0:
                     if(previousRoom.roomSize == Size.large)
                     {
                         offset = 50;
                     }
                     else if(previousRoom.roomSize == Size.medium)
                     {
                         offset = 40;
                     }
                     else
                     {
                         offset = 30;
                     }
                     newSize = Size.small;
                     break;
 
                 case 1:
                     if (previousRoom.roomSize == Size.large)
                     {
                         offset = 60;
                     }
                     else if (previousRoom.roomSize == Size.medium)
                     {
                         offset = 50;
                     }
                     else
                     {
                         offset = 40;
                     }
                     newSize = Size.medium;
                     break;
 
                 case 2:
                     if (previousRoom.roomSize == Size.large)
                     {
                         offset = 70;
                     }
                     else if (previousRoom.roomSize == Size.medium)
                     {
                         offset = 60;
                     }
                     else
                     {
                         offset = 50;
                     }
                     newSize = Size.large;
                     break;
             }
 
             switch (randomIntDirection)
             {
                 case 0:
                     newPos.z += offset;
                     break;
 
                 case 1:
                     newPos.z += -offset;
                     break;
 
                 case 2:
                     newPos.x += offset;
                     break;
 
                 case 3:
                     newPos.x += -offset;
                     break;
             }
             Room newRoom = new Room (newSize, newPos);
 
 
             //What I want but can't do :(
             if(!rooms.Contains (newRoom.roomPos))
 
             //What i can do but doesn't work how i want because its checking the size also and I only want it to check the position 
             if(!rooms.Contains (new Room(newSize, newPos)))
             {
                 rooms.Insert (0, newRoom);
             }
         }
     }
 }
 
 
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

  • Sort: 
avatar image
2
Best Answer

Answer by ShadyProductions · Jun 28, 2017 at 08:22 AM

You can use LINQ for this.

 if(!rooms.Any(f => f.roomPos == newRoom.roomPos)) {
 //If there is not any room in the rooms list, that has the same roomsize as newRoom then..
 }

or

  if(rooms.All(f => f.roomPos != newRoom.roomPos)) {
  //If all rooms in the rooms list, don't have the same roomsize as newRoom then..
  }
Comment
Add comment · Show 3 · 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 Trevdevs · Jun 28, 2017 at 08:45 AM 0
Share

Never heard of LINQ what is it lol? just get

error System.Collections.Generic.List doesn't support a definition of Any()

is it like a refernce i have to add to using or what is it? :) glad you understood though

avatar image ShadyProductions Trevdevs · Jun 28, 2017 at 08:47 AM 0
Share

You have to add

 using System.Linq;

Linq are basically a bunch of extension methods that can do alot of things while writing alot less code. https://docs.microsoft.com/en-us/dotnet/csharp/program$$anonymous$$g-guide/concepts/linq/getting-started-with-linq

avatar image Trevdevs · Jun 28, 2017 at 08:47 AM 0
Share

Figured it out

System.Linq

works like a charm thanks :D

Follow this Question

Answers Answers and Comments

66 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

Related Questions

Access list storing custom class variables from another script 1 Answer

Comparing two lists to find the difference 1 Answer

A node in a childnode? 1 Answer

Vector3 List Find Contains Specific X Coordinate Value and Remove 0 Answers

How do i remove a list item from the scene while adding the next? 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