简体   繁体   中英

Is it possible to add two parameters to a list in C#?

I am trying to figure out how to add two parameters to a list so I can have them in the same list. I have a list of gameobjects and I want to "pair" a timer to that gameobject so I can see how long it has been in the trigger. I obviously still need to do the timer and stuff (if you have any suggestions on how to do it, please tell me) along with the trigger exit and stuff but here is my code:

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


public class Grill : MonoBehaviour
{
    public int time = 0; // The amount of time the object was collided with;
    public int maxTime = 20; // The amount of time before the loop stops.

    public List<GameObject> grilledItems = new List<GameObject>();

    private void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.CompareTag("Cookable"))
        {
            grilledItems.Add(other.gameObject);
        }
    }
}

Use a tuple :

new List<(GameObject gameObj, Timer associatedTimer)>();

I don't think you should make it so complicated. If you need it, use a Map . If not structs are your friends.

public struct GrilledItemType
{
    public GameObject grilledObject;
    public Timer      timer;
};

Then just

public List<GrilledItemType> grilledItems = new List<GrilledItemType>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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