I've read through several posts about prefabs and scripts, but was hoping for some clarification. Here's an example of the problem I'm trying to solve:
Suppose I have several types of vehicles. Each vehicle can have say...1-3 launchers. Each launcher fires a different projectile type.
I want to be able to assign whatever type of launcher to the launcher "slots", and assign whatever type of projectile I want to each launcher. I also need their scripts to be able to communicate between each other.
So, I have these assets:
(Vehicles) Bigum Tank Prefab - Fixed to always have 3 launcher positions.
(Launchers I will assign to the positions, in the editor) Launcher Type 1 Prefab Gun Type 1 Prefab Launcher Type 2 Prefab
(Projectiles I will assign to the launchers) Rocket Type 1 Prefab Rocket Type 2 Prefab Bullet Type 1 Prefab
From what I understand, each prefab will have a visual aspect (the model), and the script.
Now, At this point, I want to do all of this in the UI. I don't want to dynamically "create" the tank setup at runtime, I'm ok for now just creating the tank prefab with 3 launcher positions, and dragging the launchers to each position, and dragging each projectile over also. From then on, for the life of the game, the Bigum Tank will always have this configuration.
Now, to the meat of my question. I'm obviously going to need my vehicle to communicate with the launcher scripts, as in when to fire. And the launchers are going to need to communicate with the projectile scripts, telling THEM when to fire.
Should my inspector for the Bigum Tank prefab look like this:
First Launcher..........Rocket Type 1 Prefab
First Launcher Script...Rocket Type 1 Script
Second Launcher.........Rocket type 2 Prefab
Second Launcher.........Rocket Type 2 Script
etc? Should I have a public variable reference for both the prefab, and the attached script so that my vehicle script can call methods in the launcher scripts, and so forth? I'd like to not only assign the prefab to the main object, but also to communicate between scripts.
Sorry if this is a long-winded post for something I could have stated much simpler, I wanted to make sure my intent was clear. I've setup a test project where I have prefab A with a reference to the script in prefab B. Should I just then in script B just create an instance of prefab B? That would be hardcoding in the script what type of object to create, which seems odd. Ultimately, it seems that I would want these classes to derive from a base class.
Any suggestions on how to approach this would be very helpful! Thank you
asked Mar 31 '10 at 12:58 AM