0
votes

I will use Pokemon as an example: I have a list of 10 elements, each element contains: string name, GameObject, int hp and mana int, int rarity.

I need after every click or tap, is made a random, even so good, but imagine that these 10 Pokemons, 2 of them are very rare.

after the random, will check out common or rare pokemon. if common, it will be made another radom and will choose only 1. if rare pokemon, choose one of two available. I believe it is very confusing.

the problem and I'm not managing to make the random list not instantiate the object. currently my code this as follows ..

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

public class Pokemons : MonoBehaviour 
{

    [Serializable]
    public class ListPokemons
    {
        public string name;
        public GameObject componentObjc;
        public int hp;
        public int mana;
        public int rarity;    
    }

    public ListPokemons[] pokeAtributs;

    // Use this for initialization
    void Start () 
    {
        List<ListPokemons> list = pokeAtributs.ToList ();
        //ListPokemons pokemon = list.FirstOrDefault (r => r.componentObjc != null);
    }
}

I'm using Unity 5. I am Portuguese and all the text has been translated using google translate.

2
Hint: Rare elements are a random number between 100 and 10,000(low possibility of getting the same number twice) then assign a flag as rare. Common elements is a random between 0 and 100(higher possibility), also assign a flag common. - Khalil Khalaf
You need distribution (math). But, what I tend to do in cases like this is I decide how many rare pokemons and how many common I will give out. Let's say 98 common, 2 rares. I do 2 random rolls 1-100 to decide where the rares go, or even build a fix size list, filling up accordingly and not rolling game time, just picking out the next list item; this way you will be "even-handed" as otherwise (also math: fair coin) you might won't be. BTW why ListPokemons is not a struct instead? Also use Awake() instead of Start() if you manipulate GObjs - user2299169

2 Answers

0
votes

I sounds like you should organize your rare and common in two different lists.

Something like this maybe (it's a mockup):

// fill these in the inspector. you don't need the extra arrays.
public List<ListPokemons> rarePokemon;
public List<ListPokemons> commonPokemon;

...

void PickPokemon()
{
    int rarityRoll = Random.Range(0, 100);
    if(rarityRoll < 80)
    {
        // choose something from common
        int roll = Random.Range(0, commonPokemon.Count);
        // instantiate
        Instantiate(commonPokemon[roll].componentObjc, new Vector3(5, 5, 5), Quaternion.identity);
    }
    else
    {
        int roll = Random.Range(0, rarePokemon.Count);
        // instantiate
        Instantiate(rarePokemon[roll].componentObjc, new Vector3(5, 5, 5), Quaternion.identity);
    }
}

If you have another list you can add another roll around this.

And I don't think you need to actually initialize the public arrays to a size via code. That can be done in the inspector. (Edit: Actually you must not do that because you want to populate the list before runtime.)

Random.Range(int a, int b) is from a (inclusive) to b (exclusive).

Edit 2:
I guess since your arrays will be converted to lists anyways you can just get rid of the arrays and make the lists public or work with the arrays directly and remove the lists (in this case you need to use Length instead of Count).

0
votes

The problem is that public ListPokemons[] pokeAtributs; is never initialized before you did List<ListPokemons> list = pokeAtributs.ToList ();

Simply initialize the array with the new keyword then it should work.

[Serializable]
public class ListPokemons
{
    public string name;
    public GameObject componentObjc;
    public int hp;
    public int mana;
    public int rarity;    
}

public ListPokemons[] pokeAtributs;

// Use this for initialization
void Start () 
{
    //You are missing this part in your code (10 pokeAtributs  )
    pokeAtributs = new pokeAtributs[10];

    //Now you can do your stuff
    List<ListPokemons> list = pokeAtributs.ToList ();

}