Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nephtis · Apr 03, 2019 at 08:16 PM · prefabprefabsprefab-instance

Creating a weapon database from prefabs

Currently I've created some weapons (e.g. longsword, spear) as prefabs. These consist of a sprite with a Weapon script attached, specifying attributes about the weapon (weight, cost, damage etc.)

What I want is to allow the player to:

1. Have multiple "local" instances of these weapons, NOT just references to the prefabs.

2. Customise these "local" weapons by changing their properties (e.g. adding fire damage).

But I'm struggling to do this because any changes I make to weapons affect the base prefab, which I don't want.

I know that GameObject.Instantiate creates a copy of the prefab in the scene; ultimately I do this when I want a weapon to actually be equipped, but it seems silly to instantiate all my weapons in the scene and have all but one of them sitting somewhere deactivated. The other thing I was thinking is that I could have a list of the player's current weapons, which consists of deep copies of the prefabs, and operate on that.

Basically if I have a longsword in my inventory, I don't want my customisations to that longsword to apply to ALL longswords (by affecting the base prefab), I only want it to affect that particular longsword. What's the best way of doing this?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · Apr 04, 2019 at 12:23 AM 0
Share

The problem is that you are thinking of your visual weapons as the same as the idea of your weapons. You need to separate these ideas and work on them this way. Think of it like this, what the player sees is just a visual representation, so all your weapons should just be data objects with all the information required to build the weapons at runtime, like a reference to the base prefab, damages, attack speeds, associated animations, icons, etc. This way when you want to represent an item you can call to the data object and use it as needed, if its to display in inventory, you'll use all your information to feed into your UI, and if its being equipped, you can used the data object to adjust things about the player, and so forth.

avatar image Nephtis RobAnthem · Apr 04, 2019 at 08:42 AM 0
Share

Ok, so you also favour the "deep copies" approach, i.e. have a data structure of the player's current Weapons (attributes and sprite renderer) in the Weapon$$anonymous$$anager, which is basically a list of deep copies of prefabs so that we don't operate on the prefabs, and then instantiate those copies when a weapon is equipped. Is this more or less what you're suggesting?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Esteem · Apr 03, 2019 at 09:01 PM

well since you have to have an InventoryItem class of some sort for each weapon anyway, why not store the local changed values to that and when the player chooses to equip it, just Instantiate the model for it from the prefab and override the default values by the values you have stored in your InventoryItem ?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nephtis · Apr 04, 2019 at 08:38 AM 0
Share

This is what I was thinking with the second approach: "have a list of the player's current weapons, which consists of deep copies of the prefabs, and operate on that." Each weapon prefab is a GameObject with a Weapon script storing its attributes, and a sprite renderer for what it shows up as in the scene. So I guess what I'd do in my Weapon$$anonymous$$anager class is store a list of all base prefabs, and then have a separate list of deep copies of them which acts as the player's current weapons. This list would basically store Weapons, i.e. attributes, and whenever a weapon is equipped from this list, I'd go and look up the base prefab, instantiate it, and overwrite that instance's Weapon attributes with the player's current ones. Is (something like) this what you're suggesting?

avatar image Esteem Nephtis · Apr 04, 2019 at 09:22 AM 1
Share

Depends on what you mean by a deep copy, because it seems redundant to me to have two classes that both hold very similar data.

what I would do is create an abstract WeaponData class and then use it as a base for all my weapons (i.e.: LongswordData : WeaponData

then WeaponData would have protected vars that are the same for all weapons (like damage)

LongswordData would then have longsword specific data

you can then store all the weapons in a List and when a player equips a weapon, Instantiate a prefab of that weapon with a small WeaponBehaviour : $$anonymous$$onoBehaviour class that has a field for WeaponData. (private WeaponData data;)
Where you decide to create the logic for the weapons themselves is up to you, but if you decide to to the logic in WeaponData public virtual void Attack()

You can then do public override void Attack() on Longsword and from WeaponBehaviour's Update() you can do

private void Update() {
data.Attack(); }



That way, everything is nice and structured.

NOTES: if you're serializing the weapons and saving them in a WeaponData list, careful there for Unity does not serialize base reference of inheritors.
PS.: idk how well do you know the concepts so I am sorry if this feels like I am patronizing you or whatnot :/

avatar image Nephtis Esteem · Apr 04, 2019 at 11:20 AM 0
Share

Thanks for your ideas; I also agree it's redundant to have basically two lists of a class that do exactly the same thing but just get treated differently. I like your idea of having a base WeaponData class and then having instances of that in derived classes; I'll have a play around with it when I get back to my PC.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Link GUIText to a prefab? 2 Answers

Zombie is working fine but it's prefab is creating issue in animation in unity,Zombie is working fine but it's prefab is creating issue in animation in unity Ask Question 0 Answers

Prefabs won't change? 1 Answer

Problem: Random Instantiating more than one prefab? 1 Answer

Making multiple instances of a prefab,Instantiating multiple instances of a prefab 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges