Home > database >  I'm trying to assign and output to console List of strings from another List of objects. But it
I'm trying to assign and output to console List of strings from another List of objects. But it

Time:11-15

I'm trying to assign lure List to my Fish objects in fish objects list and output to console . But it continues to duplicate strings (lures) randomly no matter what. I really find no way to fix it. Logically it must work >_>

logic:

  • generate 3 fishes with names from array and add them to List of generated fishes.
  • assign List of lures based on name of fish to newly created fish.
  • Output it in console by Debug.Log (i'm using Unity).

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

public class Test : MonoBehaviour
{
    List<Fish> generatedFish;
    string fishName;
    string[] fishNames = {"Karas", "Karp", "Ploc" };
    List<string> asignedLures;

    // Start is called before the first frame update
    void Start()
    {
        generatedFish = new List<Fish>();
   

        generateFish();
       
        foreach(Fish fish in generatedFish)
        {
            Debug.Log(fish.fishName);
            foreach(string lure in asignedLures)
            {
                Debug.Log(lure);
            }
        }
        
    }
    class Fish
    {
        public string fishName { get; set; }
        public List<string> lures { get; set; }
    }
    public void generateFish()
    {
        int numberOfGeneratedFish = 2;

        for(int i = 0; i <= numberOfGeneratedFish; i  )
        {
            generatedFish.Add(new Fish
            {
                fishName = AsignName(),
                lures = AsignLure(fishName)
            });
        }

    }
    public string AsignName()
    {
        fishName = fishNames[Random.Range(0, fishNames.Length)];
        return fishName;
    }
    public List<string> AsignLure(string fishName)
    {
        

        switch (fishName) {
            case "Karas":
                asignedLures = new List<string> {"corn", "dough", "worms" };
                break;
            case "Karp":
                asignedLures = new List<string> { "potato", "corn", "pea" };
                break;
            case "Ploc":
                asignedLures = new List<string> { "perlovka", "mastique" };
                break;
        }
        return asignedLures;
    }
}

Code generates Fish and output them in console. But in outputs wrong lure list. Also it always same for all fishes, but differs every run. Like this time all fish will have "potato", "corn", "pea". But another time it'll have "corn", "dough", "worms".

Help me to fix it please T_T. I find nothing similar in internet.

CodePudding user response:

Well you replace the content of asignedLures so of course it will be the same list for each iteration

I think you rather want to access the list stored in your Fish instances

foreach(var fish in generatedFish)
{
    foreach(var lure in fish.lures)
    { 
        Debug.Log(lure);
    }
}

Actually I don't see a reason to have that assignedLure field at all.

I would rather simply directly return the list like e.g.

public List<string> AsignLure(string fishName)
{
    switch (fishName)
    {
        case "Karas":
            return new List<string> {"corn", "dough", "worms" };
       
        case "Karp":
            return new List<string> { "potato", "corn", "pea" };
         
        case "Ploc":
            return new List<string> { "perlovka", "mastique" };
    }

    // Depending on whether you want to actively handle the exception/that case either 
    return null;

    // Or if you want a "valid" return list but simply empty
    return new List<string>();
}

Same also for

public string AsignName()
{
    return fishNames[Random.Range(0, fishNames.Length)];
}
  • Related