Script values change on play

Hi everyone, I have this script below which puts elements in the inspector to spawn, but when hitting play, the values change every time.

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

public class PokemonGenerator : MonoBehaviour
{
    public int[] Pokemons;
   
    public int maxLevel;

    public int minLevel;

    public int maxPokemonNum;

    private List<PokemonIdentity> pokemonIndentities;
    void Start()
    {
        Pokemons = Game.PokemonsData.Keys.ToArray();
       
        pokemonIndentities = new List<PokemonIdentity>();
        for (int i = 0; i < Pokemons.Length; i++)
        {
            Pokemon pokemon = new Pokemon(Pokemons[i], Random.Range(minLevel, maxLevel + 1));
            var obj = GameResources.Pokemons[pokemon.ID];
            GameObject poke=null;
            Vector2 pos = Random.insideUnitCircle*GetComponent<SphereCollider>().radius;
            pos.x += transform.position.x;
            pos.y += transform.position.z;
            if(obj.Length==1)
               poke  = Instantiate(obj[0],new Vector3(pos.x,transform.position.y,pos.y),Quaternion.identity);
            else if (obj.Length == 2)
                poke = Instantiate(obj[pokemon.isMale ? 0 : 1],new Vector3(pos.x,transform.position.y,pos.y),Quaternion.identity);

            if (poke != null)
            {
                var identity = poke.gameObject.AddComponent<PokemonIdentity>();
                identity.pokemon = pokemon;
                pokemonIndentities.Add(identity);
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}


This is how I want it


This is how it fills on play

Thanks in advance

Can you explain what this line (21 in ur code) does? Pokemons = Game.PokemonsData.Keys.ToArray();

To be honest, the source was gotten off of GitHub and some of the comments in code are in Chinese so it’s been hard to follow, but there’s a json file containing all of the info of each Pokemon (I won’t put all of them since it’s extremely long but:

[
    {
        "type1": 11,
        "type2": 3,
        "innerName": "Bulbasaur",
        "Gender": 2,
        "Ability1": 0,
        "Ability2": 1,
        "AbilityHidden": 2,
        "ID": 1,
        "EvoChainID": 0,
        "Type": [
            11,
            3
        ],
        "CatchRate": 50,
        "Height": 0.7,
        "Weight": 6.9,
        "BaseFriendship": 0,
        "LevelMoves": {
            "9": [
                73
            ],
            "6": [
                74
            ],
            "3": [
                22
            ],
            "0": [
                1,
                45
            ],
            "12": [
                75
            ],
            "15": [
                79,
                77
            ],
            "18": [
                402
            ],
            "21": [
                36
            ]
        },
        "MaleRatio": 88,
        "BaseExpYield": 0,
        "BaseStatsHP": 45,
        "BaseStatsATK": 49,
        "BaseStatsDEF": 49,
        "BaseStatsSPA": 65,
        "BaseStatsSPD": 65,
        "BaseStatsSPE": 45,
        "evYieldHP": 0,
        "evYieldATK": 0,
        "evYieldDEF": 0,
        "evYieldSPA": 0,
        "evYieldSPD": 0,
        "evYieldSPE": 0,
        "GrowthRate": 0,
        "Species": 0,
        "EvolutionMethod": 0,
        "BasicExp": 0,
        "EVYield": [
            0,
            0,
            0,
            0,
            0,
            0
        ]
    },

I’m assuming it pulls from this since it’s a pokemons.json in a PokemonData folder

I was just trying to make u see the error, what you are doing with your array is setting it in a way that you can change on inspector (making it public with a type that unity can serialize) BUT in START (that is a message that unity runs at start for each monobehaviour) you are saying "thrown off any value that is set there by inspector and add these new values that will come from Game.PokemonsData.Keys.

For this reason ur array is changing when you start the game

2 Likes

ahh!! removing that fixed it. thank you!!