sorry for my odd terminology, I am rather new to all this
I'm creating a collection/pokemon type game. as of right now I have created a separate GameObject prefab for every monster/character in the game. Each gameobject contains a script with enumerated values for it's stats. When combat begins It calls a random monster from a pool of gameobjets. The problem is at this point I'm not sure how to have it create a new instance of the enemy in a way where it retains the values in it's public floats from the prefab or have it's level be adjusted as it enters. And I don't know how I can derive it's stats and properties for scripts outside the function that spawned it. as the function that creates the new duplicate won't remember the new creature after the function resolves.
so In short, How can I reference the enumerated integers of a newly duplicated gameobject
I was asked to include some more information on what I've coded:
public List<baseMonsters> allMonsters = new List<baseMonsters> ();
public void EnterBattle(Rarity rarity)
{
// this code swaps camera view to the battlefield from the overworld map
playerCamera.SetActive(false);
battleCamera.SetActive(true);
// call function to determine which monster appears
baseMonsters battleMonster = GetRandomMonsterFromList(GetMonsterRarity(rarity));
// returns which monster was chosen
Debug.Log (battleMonster.name);
// prevents the player from moving after combat begins
player.GetComponent<PLayermove>().isAllowedtoMove = false;
//Creates the defending monster
GameObject dMon = Instantiate (emptyMon, defencePodium.transform.position, Quaternion.identity);
//Ensures scale from original is preserved
dMon.transform.localScale = battleMonster.transform.localScale;
Vector3 MonLocalPos = new Vector3 (0, 1, 0);
dMon.transform.parent = defencePodium;
dMon.transform.localPosition = MonLocalPos;
//Adds the stat components of the original prefab
baseMonsters tempMon = dMon.AddComponent<baseMonsters> () as baseMonsters;
tempMon = battleMonster;
dMon.GetComponent<SpriteRenderer> ().sprite = battleMonster.image;
bm.ChangeMenu (BattleMenu.Selection);
}
the issue that I see with the code is that it's transforming an already existing object into what the prefab monster is, not copying it. what method can I use to copy the prefab in a way I can easily reference in the same function to change it's level?
-Update-
ok so changing everything over to instantiating a GameObject is what I want. now there is the other part from the code before:
public List<baseMonsters> GetMonsterRarity(Rarity rarity)
{
List<baseMonsters> returnMonster = new List<baseMonsters> ();
foreach (baseMonsters Monster in allMonsters)
{
if (Monster.rarity == rarity)
returnMonster.Add (Monster);
}
return returnMonster;
}
public baseMonsters GetRandomMonsterFromList(List<baseMonsters> monList)
{
baseMonsters mon = new baseMonsters ();
int monIndex = Random.Range (0, monList.Count - 1);
mon = monList [monIndex];
return mon;
}
I was following a tutorial when I created this code, I am starting to understand more and more how spaghetti it is.
sorry for all the Alike Terms, baseMonsters is a class that houses things like the rarity and stats of the monster. These functions are searching by and for that script. How can I convert these codes over to searching the class from a list, to a Gameobject from a list, and still be able to read baseMonsters of each respective GameObject and it's rarity int?
as of right now, one workaround I can think of is to create a seperate list for each rarity. that way I can skip needing to read the rarities in the baseMonsters script entirely. But I will still need to know how I can call that class script for other things like determining level of the monster and what it's attacks will be?