簡體   English   中英

在 foreach 循環中比較游戲對象

[英]Compare Gameobjects in a foreach loop

我是一名藝術家,注定要為我的大學編寫 C# 代碼:D,我很想在這里得到幫助:)

問題:我有一個小地形,上面有兩個物體(通過拖放游戲放置),我在這里有我的循環,它必須檢查 ObjectA 是否與 ObjectB 具有相同的值。 那么我該怎么做呢? 我嘗試使用 foreach 循環,但如果其中一個具有正確的值,而不是兩者都具有正確的值,則條件為真。


目前,現場或小地形正在檢查一切。

它檢查是否有水,如果為真 -> 繼續,否則返回,

如果它有兩個對象,如果為真 -> 繼續,否則返回

如果這兩個對象具有相同的值,則返回,如果一切都正確,則放置 object ......我被困在最后一部分 - >比較兩個對象。

Each Object has a value, an integer, there are for different objects... object one has value 0, object 2 value 1 etc... but the player should only combine object - value 0 with object - value 0. Which means, if I drag object - value 0 and object - value 1 onto the field, nothing happens, only if I drag object - value 2 and object value - 2 onto the field or object - value 3 and object value - 3.....


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Plantgrowing : MonoBehaviour
{
    [SerializeField] LayerMask layer, layer2;
    [SerializeField] GameObject plantPref;
    GameObject plant;
    bool compatible = false;
    float waterlvl;
    private void Update()
    {
        Collider2D[] hitInfo = Physics2D.OverlapCircleAll(transform.position, .5f, layer);
        Collider2D[] hitInfo2 = Physics2D.OverlapCircleAll(transform.position, .5f, layer2);

        foreach(Collider2D hit in hitInfo)
        {
            if (hit && hit.gameObject.CompareTag("Field"))
            {
                waterlvl = hit.gameObject.GetComponent<Fields>().waterLevel;
            }
        }

        if (hitInfo2.Length == 2)
        {
            foreach (Collider2D hit2 in hitInfo2)
            {
                if (hit2.gameObject.GetComponent<Crop>().cropID == 1) //cropID is only a script with one single variable
                {
                    compatible = true;
                }
                else return;

                if (waterlvl >= 5 && compatible)
                {
                    if (!plant) plant = Instantiate(plantPref, new Vector3(transform.position.x, transform.position.y, -2), Quaternion.identity) as GameObject;
                    Destroy(hit2.gameObject);
                }
            }
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawWireSphere(transform.position, .5f);
    }
} 

由於您最多只有兩個對象(並且您專門檢查),您可以跳過 foreach 循環並直接通過它們的索引來處理它們

if (hitInfo2.Length == 2)
{
    if (hitInfo2[0].gameObject.GetComponent<Crop>().cropID == hitInfo2[1].gameObject.GetComponent<Crop>().cropID)
    {
       //they match
    }
}

如果您想要更長的列表並且想要比較所有元素,則問題的復雜性會增加。 有很多解決方案(這一切都取決於問題),但在你的情況下,這就足夠了:

int crop = -1;
bool compatible = true;

foreach (Collider2D collider in hitInfo2)
{
    int candidate = collider.gameObject.GetComponent<Crop>().cropID;
    if (crop == -1)
        crop = candidate; //initialize 'crop' variable if this is the first object
    else
        compatible = crop == candidate //check if current object is the same as initial

    if (!compatible) break; //stop looping if we found an incompatible object
}

if (compatible)
    ...    //compatible now stores true/false depending on the result

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM