Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
  • Help Room /
avatar image
1
Question by moschov · Dec 08, 2020 at 03:57 PM · coroutinelists

Dynamically update the elements of a List

I have a list of game objects for which I calculate their distance from a certain point in the Update function while moving towards it. I want to add those distances in a List where e.g on list position 0 i will have the distance of the first object from the point and it will be getting updated while the object is moving towards it. I tried doing it with both the simple list.Add(distance) and list.Insert(0, distance) in the Update() functionbut it just keeps adding the distance of each frame in the list as expected. I tried using a coroutine but I can't seem to find the right parameters for it in order to work. Long story short, if I have 5 objects I need a 5-element-list where the distance of each object is stored in a specific place and it keeps getting update while the object is moving.

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 Owen-Reynolds · Dec 09, 2020 at 06:03 PM 0
Share

So, you always have a size-5 list; you want each to store its distance to the player; and it should always be sorted by that distance?

avatar image moschov Owen-Reynolds · Dec 10, 2020 at 12:03 AM 0
Share

I haven't set the list.Count to be 5. 5 was an example. To be sorted by distance is not my problem either.

The distance calculator is in my Uodate function. So for every frame I have a different distance while the object is moving towards the point. I have multiple objects like that. I want to define a distance list where for every x object there will be a distance[x] float and every frame that distance[x] is going to be updated with the more recent distance. When I'm trying to do that I get a list with all the distance floats of each object since the start of the scene. I might not be the best to explain it since I have near to zero experience in unity and in c#.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sacredgeometry · Dec 08, 2020 at 04:36 PM

It should be as simple as:

 MyList[0] = newValue;

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 moschov · Dec 09, 2020 at 05:54 PM 0
Share

Actually, I might have missed with the title but before I get to the problem of updating each element of the list is to how to actually define the list so it doesn't add the distance for each frame as a different element rather than just lock in a certain position each object' s distance.

avatar image sacredgeometry moschov · Dec 10, 2020 at 04:25 AM 0
Share

Can you define the problem more succinctly? Is it this?

On each frame I want to be able to update a list of the last n distances between two objects


If so I can amend my answer

avatar image
0

Answer by Jibinhok · Dec 10, 2020 at 04:30 AM

I think I have an idea of what you're asking. You want a list of values that change but instead you're adding new items to the list.

Instead of adding to the list in Update() you should be updating the value in the list. So add the elements to the list somewhere else in your code:

 Start() 
 {
      list.Add(objectOneDistance);
      list.Add(objectTwoDistance);
      list.Add(objectThreeDistance);
 }
 
 Update()
 {
      list[0] = objectOneDistance;
      list[1] = objectTwoDistance;
      list[2] = objectThreeDistance;
 }

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 sacredgeometry · Dec 10, 2020 at 04:41 AM 0
Share

Yep thats what I originally thought was the case. If this is the answer and the missing information was where to put code, then the OP should trying to write code and start reading. And the first place I would suggest is probably understanding unitys lifecycle events:


https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.html https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.html

avatar image
0

Answer by jacobbenjaminbanes · Apr 22 at 06:30 PM

I just finished working on something similar that I think you could easily expand on for your needs.

I had a player object keeping a list of every enemy in a certain range by using an empty child object with box collider.

The child object gets a script to update the parent.

 public List<GameObject> liveEnemies;
     private PlayerController PlayerController;
     // Start is called before the first frame update
     void Start()
     {
         PlayerController = GetComponentInParent<PlayerController>();
     }
     // Update is called once per frame
     void Update()
     {
         PlayerController.liveEnemies = liveEnemies;
     }
 private void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.CompareTag("Enemy"))
     {
         liveEnemies.Add(other.gameObject);
     }
 }
 private void OnTriggerExit(Collider other)
 {
     if (other.gameObject.CompareTag("Enemy"))
     {
         liveEnemies.Remove(other.gameObject);
     }
 }


The PlayerController script has liveEnemies publicly declared inside it as well.

This will get you a list of the gameObjects with "Enemy" tag in range of the box collider, from there you can work out the distances you wanted.

As a side note, if an Enemy object is destroyed inside the collider it won't trigger OnTriggerExit, the way I fixed this was a script on the Enemy.

 public class EnemyInstanceManager : MonoBehaviour
 {
     private sightManager playerSightManager;
     public int health = 10;
     // Start is called before the first frame update
     void Start()
     {
         playerSightManager = GameObject.FindGameObjectWithTag("Player").GetComponentInChildren<sightManager>();
     }

     // Update is called once per frame
     void Update()
     {
         // if health reaches zero, destroy object and remove from player sight list
         if (health <= 0) {
             playerSightManager.liveEnemies.Remove(gameObject);
             Destroy(gameObject);
         }
Comment
Add comment · 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

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

153 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

Related Questions

Vector 3 MoveTowards not working in my coroutine 0 Answers

Removing item from list makes it appear at the bottom ? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 0 Answers

Unity Editor - Group Public GameObjects 0 Answers

List wont appropriately add objects. 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