• 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 Feref2 · Sep 20, 2018 at 06:51 AM · updatingfloats

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 t$$anonymous$$s 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.

Comment
Add comment
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

2 Replies

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

Answer by ShadyProductions · Sep 20, 2018 at 06:58 AM

Somet$$anonymous$$ng like t$$anonymous$$s?

 using System.Linq;
 
 public float GetSumBounds(GameObject[] groundBases) 
 {
      return groundBases.Sum(f => f.GetComponent<MeshFilter>().mesh.bounds.size.x);
 }

You call t$$anonymous$$s 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 t$$anonymous$$s a lot.

Comment
Add comment · Show 4 · 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 Feref2 · Sep 20, 2018 at 08:40 PM 0
Share

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:

Mesh mesh;

Void Awake () { mesh = GetComponent().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 MeshFilters?

avatar image ShadyProductions Feref2 · Sep 21, 2018 at 06:53 AM 0
Share

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, MeshFilter> _meshFilterCache = new Dictionary<int, MeshFilter>();

 public float GetSumBounds(GameObject[] groundBases)
 {
     List<MeshFilter> meshFilters = new List<MeshFilter>();
     foreach (var gbase in groundBases)
     {
         MeshFilter 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<MeshFilter>();
             _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);
 }
avatar image Bunny83 Feref2 · Sep 21, 2018 at 09:29 AM 0
Share

Keep in mind 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.

avatar image Feref2 · Sep 26, 2018 at 12:45 AM 0
Share

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.

avatar image
0

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;
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

92 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

Related Questions

prefab updating wrong.. 1 Answer

I can't get the Editor to update to version 5.0.1 1 Answer

Replacing TerrainData#splatProtoypes in 2018.3 1 Answer

Shader float value weirdness 1 Answer

Updating from Unity 4.0 to 4.1 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