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.
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?
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#.
Answer by sacredgeometry · Dec 08, 2020 at 04:36 PM
It should be as simple as:
MyList[0] = newValue;
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.
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
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;
}
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
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);
}
Your answer
Follow this Question
Related Questions
Vector 3 MoveTowards not working in my coroutine 0 Answers
Getting variable from script while keeping other code from it 1 Answer
How to deactivate a dropdown list when changing UI tabs 0 Answers
Coroutine doesn't continue after yield another coroutine 0 Answers
Coroutine not working when another Scene is first opened. 0 Answers