Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm trying to assign and output to console List of strings from another List of objects. But it continues to duplicate strings

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)];
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading