Jump to content
  • Advertisement
Sign in to follow this  
Durakken

Unity spawn different prefabs with same script logic.

This topic is 985 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Again, back with the pkmn-esque clone game...

 

Again before starting I'm using Unity and C#...

Here's the problem I'm having now... Not really a problem so much as how should I go about doing this...

 

Like Pkmn I want to have 6 slots of monsters that I can select from... hit a button and have that monster come out and I have control of them.

 

Unity uses the code "Instantiate(Monster, Position, Rotation);" to create a prefab of "Monster"

 

Now I have however many Monster types and I need to keep track of each instance of a Monster as well obviously.

 

The problem I'm having is how to handle spawning those individual types? Where to put the Meshes, etc.

 

 

My thought is that I need to have some sort of object that has all the meshes attached to it that I call on from the Monster instantiation. It seems wrong to put it on the prefab of the monster prefab as that would make all the meshes load and unload upon creation/destruction. I suppose telling which is selected can be passed from a separate script, but it seems wrong to do this... 

 

Am I thinking in the write direction? Do you guys know? Am I just thinking its wrong cuz I'm still not used to this more Object Oriented programming style?

Share this post


Link to post
Share on other sites
Advertisement

Well, you can set up a table of prefabs and pick one from the table to spawn.

 

GameObject[] prefabs = new GameObject[6];

Once the script is attached to a game object in your scene, you should be able to expand prefabs and see 6 slots. Drag and drop your monsters in the slots in whatever order you desire, say alphabetic.

When you need to instantiate a monster, use "int monsterIndex" to select a monster from the table.

"GameObject o = Instantiate(prefabs[monsterIndex], Position, Rotation) as GameObject;"

 

Alternatively you can have a single Monster prefab with 6 monster children and toggle active on the child you wish to use.

"GameObject o = Instantiate(yourMonsterPrefab, Position, Rotation) as GameObject;"

"o.transform.getChild(monsterIndex).gameObject.setActive(true);"

Share this post


Link to post
Share on other sites

I think I see what you're saying...

 

Currently I have a 2 prefabs.

Monster (nested camera)

Player (nested camera)

 

The Player prefab loads on terrain load

The Player can press up or down which scrolls through the Slots of carried monsters

When the Player presses E it spawns the monster from the carried slots

When the Player presses Q it switches Camera between the spawned player monster and the player

 

Further Monsters should be spawning on the Map as enemies, indipendently from the Players.

-------------------

 

Now, it seems to me that the Enemy Monsters and the Player Monsters should draw from the same meshes and such, but the Player Monster needs the camera while the Enemy Monsters do not. And since it's probably a good idea not to have the same mesh loaded up twice I would think that there should be a separate prefab monster group, and then Enemy Monster Prefab and Player Monster Prefab.

 

What you seem to be saying is to have the Player Monster prefab loaded with all the meshes and a Enemy Monster prefab loaded with all the meshes rather than seperating those meshes into a seperate prefab or whatever.

 

I also intend to have these same meshes used in other areas like whatever form of a pokedex i come up with and perhaps display pictures in the selection area...so it seems to me that it'd be smart to have these referenced somewhere else...

 

Or am I completely off based and it doesn't make any difference whatsoever if I have the meshes saved somewhere or not?

What I'm thinking is that instantiating in the Player Controller Script is that I should pass an ID of some sort to another script that figures out what that's stats and such are and then selects the Monster type's index number.

Share this post


Link to post
Share on other sites

I am having trouble understanding what you are trying to achieve. Optimization? Memory use reduction? Draw call reduction? CPU load reduction?

Most common practice for developers is to avoid overusing instantiate and destroy command as much as possible as it will bog down your system. Especially for bullets and stuff, google around and you will find a few useful guides. https://www.microsoft.com/en-gb/developers/articles/week04jul15/5-common-mistakes-made-in-unity/

 

"Normal practice" for me is to create a monster prefab for a player and one for enemy. However if you wish to use a single prefab for both the enemy monster and player monster you will have to do some changes upon instantiating them. Like assigning camera script and feeding it a camera object as well as adding newly created game object to a monsterPlayer layer. Layers are a good way to differentiate between factions without having to use "GetComponent<YourScript>().getFaction()" and other cumbersome code.

 

Once again, am not really sure what you are on when you talk about sharing mesh. When you import a game model and create multiple prefab with said model as base, all prefabs will use the same mesh. So at any point you can open imported 3D model, change whatever you desire with it and these changes will apply to all prefabs. Same goes for material, if you create 1 material and give it a texture you can assign it to as many prefabs as you wish.

Edited by Zing Freelancer

Share this post


Link to post
Share on other sites

I'm trying to figure out the best way to do it, at least in terms of unity's logic without having a lot of redundancy in any place.

 

Again you seem to be saying that...

 

Player Monster prefab

-> mesh 1

-> mesh 2

 

Enemy Monster prefab

-> mesh 1

-> mesh 2

 

is the right way to go rather than...

 

Monster Meshes prefab

-> mesh 1

-> mesh 2

 

Player Monster prefab

-> Monster Mesh select from Monster Meshes prefab

 

Enemy Monster prefab

-> Monster Mesh select from Monster Meshes prefab

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!