|
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 ... etc 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
(comments are locked)
|

Actually...thinking about this...it almost sounds like I need to be able to have these as components, that I add to a vehicle prefab. I guess I would be able to add launcher components, and drag projectile prefabs to them. But would they still be able to call script functions in the other components easily?
Yes - calling functions inside other components remains easy despite a potentially confusing hierarchy of prefabs and parent/child relationships.
So, if I want any of my ship prefabs (which contain the ship models) to have assignable launchers and projectiles which can be assigned to the launchers, I'm still a little confused on what approach to take. So far, the only approach I have is for my main prefab script to declare these lists: List LauncherPrefab; List ProjectilePrefab; List LauncherScript; List ProjectileScript;
This way I can assign the launcher model to use (if any), and the scripts also. But, I have to create a generic gameobject for the scripts to be contained it, which=odd