- Home /
How can I get the updated sum of a list of floats?
So it´s basically getting a total sum that updates every time the groundBases´size changes
for (int i = 0; i < groundBases.Count; i++)
{
allGroundBases += groundBases[i].GetComponent<MeshFilter>().mesh.bounds.size.x;
//groundBases[] are gameObjects, but am adding only the size.x float values
}
I thought that this would be enough, but it seems that it updates too much and never stops increasing. I know that is because it keeps its actual size and then adds again at every frame, but I don´t know how to reset it after it changes (or whatever I have to do) and stop it when all the groundBase sizes have been added. Thanks in advance.
Answer by ShadyProductions · Sep 20, 2018 at 06:58 AM
Something like this?
using System.Linq;
public float GetSumBounds(GameObject[] groundBases)
{
return groundBases.Sum(f => f.GetComponent<MeshFilter>().mesh.bounds.size.x);
}
You call this method everytime u want to know the updated sum,
also you might want to find a way to cache the meshfilters if you're gonna call this a lot.
Thanks, I think this works. And I knew it would be good to cache the meshfilters, but even before I asked this question I tried and it didn´t work. I think I did this:
$$anonymous$$esh mesh;
Void Awake () { mesh = GetComponent<$$anonymous$$eshFilter>().mesh; }
But maybe it didn´t work because was interpreted as "THIS" GetComponent and the components I want to get are from gameObject lists? No, seriously, how can I cache the $$anonymous$$eshFilters?
Since the groundBases you're passing to the method can be different each time, it means they can sometimes be different objects that we haven't cached yet, so we will have to dynamically cache the objects, and verify if we have already some for an existing object.
We can probably do this using a dictionary and the unique id from the gameobject like so:
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
private readonly Dictionary<int, $$anonymous$$eshFilter> _meshFilterCache = new Dictionary<int, $$anonymous$$eshFilter>();
public float GetSumBounds(GameObject[] groundBases)
{
List<$$anonymous$$eshFilter> meshFilters = new List<$$anonymous$$eshFilter>();
foreach (var gbase in groundBases)
{
$$anonymous$$eshFilter filter;
int goId = gbase.GetInstanceID(); // Unique id for the gameobject
// Try get the meshfilter out of the cache, if it's not in we add it
if (!_meshFilterCache.TryGetValue(goId, out filter))
{
filter = gbase.GetComponent<$$anonymous$$eshFilter>();
_meshFilterCache.Add(goId, filter); // Add it to the cache
}
// Add to the list so we can check the sum of all
meshFilters.Add(filter);
}
return meshFilters.Sum(f => f.mesh.bounds.size.x);
}
$$anonymous$$eep in $$anonymous$$d that mesh.bounds are in localspace and usually don't change unless you actually modify the mesh and recalculate the bounds. That means scaling the objects will not change the values you get. If you want the actual size in world units you have to use renderer.bounds. Those are world axis aligned and represent the actual size.
Sorry for not asking this before, but I was busy with other stuff; but I have trouble understanding how necessary are both the Dictionary and the id? Why is not enough doing a foreach? Besides that, am almost completely sure that if I want to make other floats like this one I should create new Dictionaries, or can I reuse that one? Thanks for your answer anyway.
Answer by LCStark · Sep 20, 2018 at 10:08 PM
float groundBasesTemp = 0.0f;
for (int i = 0; i < groundBases.Count; i++)
{
groundBasesTemp += groundBases[i].GetComponent<MeshFilter>().mesh.bounds.size.x;
//groundBases[] are gameObjects, but am adding only the size.x float values
}
allGroundBases = groundBasesTemp;
Your answer
Follow this Question
Related Questions
Why have newly added character animations made in blender stopped appearing in unity. 0 Answers
Variable decreases multiple times 0 Answers
Enabling input 2 Answers
OnGUI and Displaying GUI Features 1 Answer