- Home /
reposting in a more general topic
Start function on a listed non mono script?
I'm trying to create an editable list from the inspector, which calls on another list from another object. I am having a problem with the LearnableMoves function not being called, and i'm wondering why it's not running and how I could get around this.
public List<LearnableMoves> learnableMoves = new List<LearnableMoves>();
[System.Serializable]
public class LearnableMoves
{
private GameManager gm;
private GameObject gameManager;
public int LearnedLevel;
public int MoveSelectionCaller;
private string MoveSelectionCallerT;
public PokemonMoves pokemonmove;
public string pokemonmovename;
public int pp;
public LearnableMoves(){
gameManager = GameObject.FindGameObjectWithTag ("GameManager");
gm = gameManager.GetComponent<GameManager>();
pokemonmove = gm.allMoves [1];
pokemonmovename = pokemonmove.Name;
pp = pokemonmove.PP;
}
}
Answer by ray2yar · Jan 21, 2019 at 01:56 PM
It looks like your "learnablemoves" is setup to initialize a custom class. So it would only be called when you write something like this elsewhere:
LearnableMoves MyMoves = new LearnableMoves();
That said, I think you should pass your class some information rather than assume it all... maybe like this:
public LearnableMoves(int level, int select, PokemonMoves moves, string name, int p)
{
//assign all these passed variables to your public declarations
}
If you switched to this then in your other script when you initialize the class you'd write something like this:
LearnableMoves MyMoves = new LearnableMoves(5, 1, //pokeman moves?, "pikachu", 3);
Answer by jiggish · Jan 22, 2019 at 05:59 AM
@ray2yar this is my first time using the forums, so idk if I have done everything correctly, but I do use the LearnableMoves at the beginning of the script in a list, which I have to keep. The problem is this script is copied multiple times so I have to keep it ambiguous, and I can't create all the different MyMoves methods. I realize I forgot to mention this, but allMoves is another list that I have already defined under another game object, and that's where most of my problem is. I'm trying to call on the list created there, and I can't find out how to, as I can't make this script mono or it won't let me edit the variables from the inspector, and I can't find any way to run a function from this script. Thank you for your help though, and I will attach the full script , as well as the other list it calls on below.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class BasePokemon : MonoBehaviour {
public string PName;
public Sprite image;
public BiomeList biomeFound;
public PokemonType type;
public Rarity rarity;
public int HP;
private int maxHP;
public Stat AttackStat;
public Stat DefenceStat;
public List<LearnableMoves> learnableMoves = new List<LearnableMoves>();
public PokemonStats pokemonStats;
public bool canEvolve;
public PokemonEvolution evolveTo;
private int level;
void Start () {
maxHP = HP;
}
void Update () {
}
public void AddMember(BasePokemon bp)
{
this.PName = bp.PName;
this.image = bp.image;
this.biomeFound = bp.biomeFound;
this.type = bp.type;
this.rarity = bp.rarity;
this.HP = bp.HP;
this.maxHP = bp.maxHP;
this.AttackStat = bp.AttackStat;
this.DefenceStat = bp.DefenceStat;
this.pokemonStats = bp.pokemonStats;
this.canEvolve = bp.canEvolve;
this.evolveTo = bp.evolveTo;
this.level = bp.level;
}
}
public enum Rarity
{
VeryCommon,
Common,
SemiRare,
Rare,
VeryRare
}
public enum PokemonType
{
//removed to meet character limit, just a list of types
}
[System.Serializable]
public class PokemonEvolution
{
public BasePokemon nextEvolution;
public int levelUpLevel;
}
[System.Serializable]
public class PokemonStats
{
public int AttackStat;
public int DefenceStat;
public int SpeedStat;
public int SpAttackStat;
public int SpDefenceStat;
public int EvasionStat;
}
[System.Serializable]
public class LearnableMoves
{
private GameManager gm;
private GameObject gameManager;
public int LearnedLevel;
public int MoveSelectionCaller;
public PokemonMoves pokemonmove;
public LearnableMoves(){
gameManager = GameObject.FindGameObjectWithTag ("GameManager");
gm = gameManager.GetComponent<GameManager>();
pokemonmove = gm.allMoves [MoveSelectionCaller];
}
}
public List<PokemonMoves> allMoves = new List<PokemonMoves>();
[System.Serializable]
public class PokemonMoves
{
public string Name;
public MoveType category;
public Stat moveStat;
public PokemonType moveType;
public int PP;
public float power;
public float accuracy;
}
the list and everything thereafter is on a separate script
As far as I can see, even in that script you never initialize your learnablemoves. To add to the list you would have to do something like this:
Learnable$$anonymous$$oves AName = new Learnable$$anonymous$$oves(**pass variables**);
learnable$$anonymous$$oves.Add(AName);
It's added in the inspector. I was able to solve the problem here, I realize I was asking too broad a question. What I'm looking for was a way to call a function within the Learnable$$anonymous$$oves, and I was having trouble with using FindGameObjectWithTag before serialization.
Follow this Question
Related Questions
Creating a list of singletons? 1 Answer
A node in a childnode? 1 Answer
List of custom classes in MonoBehaviour's Data gets lost after Re-Opening Unity!!! 0 Answers
Accessing a MonoBehaviour List within a Non-MonoBehaviour Script 2 Answers
How can I set the properties of serialised fields on an object recusively? 0 Answers