Switching Models

Hello,

I’ve tried my best to google for this, I apologize if I used the wrong terms so it didn’t turn anything up. If this has been answered, I’d appreciate being pointed in the right direction.

I’m making my first Unity game, where the player manages a factory full of machines. A simulation game.

Machines all behave similarly. Right now I have a generic machine Prefab which is a little cube sporting a few scripts. It works just great! But… I want to have dozens of machines!

Machines will all be identical with the exception of some metadata and which model is being rendered. If I was coding, I’d have a GenericMachine class, with a property called “Model” which I would assign at runtime based on game definition files.

So, I have a GenericMachine prefab. I also have machine models my Assets folder (go Blender!). What I’d like to do from code is, right after I create an instance of GenericMachine, I assign it a different model (a non-cube) from my Assets folder. This confuses me because it’s not just an assignment, it requires some kind of reference to a whole little hierarchy worth of stuff in the asset folder.

I want to avoid having to define all the machines in Unity’s integrated editor in favour of a quick zip through an XML file.

I’m using C#, in case that makes a difference.

Thanks for your time.

Cheers,
Fletch

You could maintain a list of your models on the central spawning object and then from that, instantiate and parent the right model to the newly instantiated machine GO.

If you don’t care much for drag-dropping the models into an array, you could always write a short editor script to do it for you.

What do you mean by that? :?

n:shock:b

public GameObject[] modelList;

That’ll create an array into which you can add prefab references via the inspector.

Wow- that’s great. I had no clue!

Cool!

That’s great, I’m going to try that for a while. Thanks a lot for your help.

Fletch