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

    // 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?


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?

  • \$\begingroup\$ Inheriting public members from prefab to instantiated copy is the basic out-of-the-box behaviour. Did you find it wasn't working the way you expected when you tried it? If so, can you edit your question to show us a minimal example of the code you're using to spawn and/or configure your instances, and describe where it goes wrong? \$\endgroup\$ – DMGregory Jun 19 '18 at 3:46
  • \$\begingroup\$ It's alright to have spaghetti code at first, my first project was a mess. I think you need to simplify your code for us and take it apart a bit so that we can understand it better. Also, try not to change the question too much, it might get buried if you keep adding new stuff to an older question. The current problem you have seems to be with software design. I suggest you brush up on the matter, and as it is a huge field, try to start with the basics. I'm learning the basics about software design myself and I'm not sure if I can be of any use further ahead. \$\endgroup\$ – John Hamilton Jun 20 '18 at 16:38

As far as I can read the code, you're picking a monster from a prefab list with

baseMonsters battleMonster = GetRandomMonsterFromList(GetMonsterRarity(rarity));

Then you're creating an empty monster

GameObject dMon = Instantiate (emptyMon, defencePodium.transform.position, Quaternion.identity);

And you're trying to fill the empty monster instance with the current monster's 'attributes' with the rest of the code.

If your monsters are pre-made gameObjects themselves, then you could just create the monster you just picked, instead of dealing with the whole empty monster filling process.

GameObject dMon = Instantiate (battleMonster, defencePodium.transform.position, Quaternion.identity);

The rest of the code will need to change a bit too, but I can't really edit the whole thing because it will probably break some other functionality with the tempMon variable that you're not using in the current function.

Though these two lines shouldn't be necessary if you can instantiate the monster directly (since it will already have the image and the scale):

dMon.transform.localScale = battleMonster.transform.localScale;
dMon.GetComponent<SpriteRenderer> ().sprite = battleMonster.image;
  • \$\begingroup\$ Hey thanks, I'll give that a try and get back tommorow \$\endgroup\$ – 4thGear Jun 19 '18 at 6:36
  • \$\begingroup\$ U updated my question with the rest of the code that this interferes with. The direction you suggested is the way I want to go. but the rest of the code is still operating off of the class of the Gameobject and not the Gameobject itself. \$\endgroup\$ – 4thGear Jun 20 '18 at 4:45

See John's statement, What I decided on with the code I have is that I need to convert all the class based structure into GameObject Scructure instead. as most of my issues will likely just be cleared up by that alone.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.