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 26, 2020 at 07:27 PM by nicoolsen10 for the following reason:

I fixed it

avatar image
0
Question by nicoolsen10 · Jan 20, 2020 at 05:41 PM · script.listlists

How to add something to a list on awake

Hey how do i make my building add itself onto a list there is on another gameobject i just cant seem to find out how. this is my building script where it should add itself on awake

 public List<GameObject> buildings;
 
     void Awake()
     {
         instance = this;
         buildings = new List<GameObject>(GameObject.FindGameObjectsWithTag("ListGameObject"));
         buildings.Add(gameObject);
     } 

and this is the lists script it needs to add itself onto

     //Lists
     public List<GameObject> buildings;
     
     public void Update()
     {
 
         //Building List
         buildings = new List<GameObject>()
         {
 
         };
 
     }
Comment
Add comment · Show 2
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 EpicCrownKing · Jan 20, 2020 at 06:57 PM 0
Share

If I understand correctly, you want all objects with the first class to be added onto a list in the second class. I'm no expert with lists, but why are you initializing it in the first class, and not the second one?

avatar image nicoolsen10 EpicCrownKing · Jan 20, 2020 at 07:41 PM 0
Share

okay the thing is that the building is something you can buy so i want it to add itself to the list when it get bought and when it gets destroyed it will remove itself its a way i use to my enemies to find the nearest building to attack and i also want it the other way around so when an enemy spawns it will be added to a enemy list and get the building list and go after the nearest building and when it dies it will remove itself I hope you understand what i mean

1 Reply

  • Sort: 
avatar image
1

Answer by IINovaII · Jan 20, 2020 at 09:06 PM

Try this approach. Create something like a manager class which holds the list of buildings in game.

Manager Class

 public class BuildingManager : MonoBehaviour{
 
     private List<GameObject> _allBuildings = new List<GameObject>();
     
     public static BuildingManager Current;
     
     private void Awake(){
     
         if(Current == null)
             Current = this;
     }
     
     public void AddBuilding(GameObject building){
         if(!_allBuildings.Contains(building))
             _allBuildings.Add(building);    
     }
     
     public void RemoveBuilding(GameObject building){
         if(_allBuildings.Contains(building))
             _allBuildings.Remove(building);    
     }
 }



Sample Building Class which adds itself would look like this.

Building Class

 public class Building : MonoBehaviour{
     
     private void Awake(){
         BuildingManager.Current?.AddBuilding(this);
     }
 }
Comment
Add comment · Show 9 · 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 IINovaII · Jan 20, 2020 at 09:09 PM 0
Share

In my opinion, using Hashset ins$$anonymous$$d of List to store Buildings is much better since it won't add duplicates in there.

avatar image nicoolsen10 · Jan 21, 2020 at 03:48 PM 0
Share

Okay so this works but i want to have more of the same building but when the building get spawned in its only the first there is getting added to the list how do i make the number 2 and 3 also gets added on the list?

avatar image IINovaII nicoolsen10 · Jan 21, 2020 at 04:50 PM 0
Share
  • Do all of your building objects have Building script attached to it?

  • Is their only one Building$$anonymous$$anager object within the scene with the Building$$anonymous$$anager script attached to it?

avatar image nicoolsen10 IINovaII · Jan 21, 2020 at 05:51 PM 0
Share

Right now there is only 1 building but i want to spawn more of this 1 building example it is a orb building and i want to have 3 of them on the same time but only 1 of them gets added and there is only one building$$anonymous$$anager with the buildingmanager script on

Show more comments

Follow this Question

Answers Answers and Comments

143 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

Related Questions

Public list are not reflecting in inspector. 1 Answer

A node in a childnode? 1 Answer

how can I get values stored in a list? 3 Answers

Reading all data in the list with duplicate data inside 1 Answer

Updating second variable inside a list using c# scipt 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