Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 audibles · Oct 28, 2013 at 03:11 AM · gameobjectlistruntimegeneration

Declaring/modifying lists at runtime

Hopefully I explain this clearly enough.

Intended behavior: A character moves from room to room, visiting random objects in the room. It never visits the same object twice and may return to previously visited rooms.

Possible solution: The room carries a list of objects within it, which it passes to the visiting character. The character creates its own list of objects for that room and removes objects as they are visited.

Problem: For this solution, I would need to be able to create a list for each room at runtime (the number of rooms/objects is undefined). Is it possible to create lists in this way?

I'm trying for a clean and simple solution that's flexible enough to accommodate any number of rooms/objects. Other solutions for the behavior are welcome as well.

Thank you!

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 robertbu · Oct 28, 2013 at 04:44 AM 0
Share

Generic lists might be what you are looking for. Here is a Wiki article on several of the collection types available in Unity:

http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

1 Reply

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

Answer by Tomer-Barkan · Oct 28, 2013 at 05:59 AM

This depends a bit if you want to create the list of objects per room in the inspector in unity, or during runtime. I'll explain how to do it when you create the object list in runtime and then explain what to change if you want to manually create the object list in the inspector.

You can start by creating a List of objects per room. First, reference the generic collections:

 using System.Collections.Generic;

then, to your room class, add a new list of objects:

 List<MyObject> roomObjects = new List<MyObject>();

next, in your Start() method of the room, fill the list with object

 roomObjects = GenerateObjectList(this); // you have to implement GenerateObjectsList(Room room) to return a list of objects for the given room.

now, when the player enters the room, you will need to COPY the list, not just reference it, otherwise when you remove something from the player's list it will also remove it from the room's list.

 private Dictionary<room, List<MyObject> playerRoomObjectList = new Dictionary<room, List<MyObject>(); // this will hold the list of objects in each room as the player sees them.

 // when entering a room, check if the player already has the list of objects. If not, copy it from the room
 void EnterRoom(Room room) {
     if (!playerRoomObjectList.containsKey(room)) {
         playerRoomObjectList[room] = new List<MyObject>(room.roomObjects);
     }
 }

There, now you have a list of objects per room in your player's script, to reference a list of a specific room use playerRoomObjectList[room]. If you want to remove an object from a room list:

 void VisitedObject(Room room, MyObject obj) {
     playerRoomObjectList[room].remove(obj);
 }


Now, if you want to populate the object list per room in the inspector, simply have an array of objects per room instead of a list (since you define this in advance in the inspector, it is a constant array and not a changing list):

 public MyObject[] roomObjects = new MyObject[1];

Then, in the inspector, you can change the size of the array per room, and drag and drop MyObject scripts into it. The rest of the code should work the same.

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 audibles · Oct 28, 2013 at 07:17 AM 0
Share

The first implementation is exactly what I was looking for! I played around with the idea of using a list of lists or arrays, but the idea of using a dictionary with rooms and lists hadn't occurred to me. Thank you!

avatar image Tomer-Barkan · Oct 28, 2013 at 07:28 AM 1
Share

Glad it helped. Always keep dictionaries in $$anonymous$$d (or hashmaps in other program$$anonymous$$g languages). They are very useful and very efficient.

avatar image techshaman · Jun 26, 2014 at 02:12 PM 0
Share

Are there any performance concerns with creating lists at runtime? Is it more of an issue on mobile? Right now I use object pooling to reuse GameObjects but I am newing collections at runtime.

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

16 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

Related Questions

A node in a childnode? 1 Answer

How to pull two objects at the same time? 0 Answers

Ordering a list of GameObjects 3 Answers

Can't add GameObjects to ArrayList 1 Answer

Keep adding targets to a list 2 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