x


Question about an approach to prefabs and scripts

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

more ▼

asked Mar 31 '10 at 12:58 AM

jc_lvngstn gravatar image

jc_lvngstn
453 24 29 40

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?

Mar 31 '10 at 01:12 AM jc_lvngstn

Yes - calling functions inside other components remains easy despite a potentially confusing hierarchy of prefabs and parent/child relationships.

Mar 31 '10 at 08:39 AM qJake

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

Apr 02 '10 at 01:31 AM jc_lvngstn
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3329
x1673
x1256

asked: Mar 31 '10 at 12:58 AM

Seen: 1949 times

Last Updated: Mar 31 '10 at 12:58 AM