简体   繁体   English

如何在Unity C#中一起计算来自不同gameObjects的数字

[英]How to calculate the numbers from different gameObjects together in Unity c#

I'm building a Unity game where you have feelers, and by clicking the color given at that moment, you can get or lose a score, time or you get a game over screen. 我正在构建一个有您的触角的Unity游戏,并通过单击该刻给出的颜色,您可以得到或失去分数,时间,或者可以在屏幕上获得游戏。

My problem is - I have a lot gameObjects and each one calculates scores alone, (eache one have his own score int) and I need one code detects all scores number one, so I can see the scores one point (about how to see it, this is less important). 我的问题是-我有很多gameObjects,每个对象都单独计算得分(每个得分都有自己的得分int),并且我需要一个代码来检测所有得分为第一的数字,因此我只能看到一个得分(关于如何看待得分) ,这不太重要)。

The name of the number that I want to find is called (how creative): score. 我要查找的号码的名称称为(创意程度):得分。 And yes, this is a public float . 是的,这是一个公众持股量

I am most glad if I could write this document duration (unless you have no choice anyway ...) 我很高兴能写出这份文件期限(除非您别无选择...)

sorry about any grammar mistakes, I do my best. 对于任何语法错误,我们深表歉意。

Thanks from advance Roei Meiri. 感谢前进荣美利。

:) :)

I can't image this would be very efficient, you would probably get better performance if you keep you own list of all the scoring objects(add to the list when creating the scoring object). 我无法想象这会非常有效,如果您保留所有评分对象的列表(在创建评分对象时添加到列表中),则可能会获得更好的性能。

That being said...(uncompiled example). 话虽这么说...(未编译的示例)。
Assuming you want to find of all objects with a particular component, like 假设您要查找具有特定组件的所有对象,例如

class ScoringObject:Monobehavior

then you can find them all and add up the scores like so... 然后您就可以找到它们并像这样加总分数...

float totalscore=0;
ScoringObject[] listOfScores= Object.FindObjectsOfType(typeof(ScoringObject)) as ScoringObject[];
foreach (ScoringObject scorObject in listOfScores) {
            totalscore+=listOfScores.score;}

http://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html http://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM