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

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

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.

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:

$$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?

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, $$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);
 }
avatar image Bunny83 Feref2 · Sep 21, 2018 at 09:29 AM 0
Share

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

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.

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

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

Possible Memory Leak on Asset Server Update 0 Answers


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