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 cirrusplay · Aug 11, 2017 at 04:22 PM · prefabreferenceclone

Prefab from file reference not cloned?

I'm still getting used to the way Unity does thing. So bear with me please.

I'm currently writing an RPG/strategy-style card game. The way I have organized my card system is by creating Card objects, and each Card object has one or more Ability objects tied to it. This allows me to swap out abilities on the fly or create new card type by simply trying new combinations of abilities. Each card type and ability type has been converted into a prefab for easy reuse.

The problem I've run into is modification to an ability causes the prefab (the actual file apparently) to be altered. The example I'm running into involves abilities which can be toggled. During the game, if I toggle an ability on and then exit the game, the next time I start the game all of my cards with that ability will suddenly have it initially toggled on. This seems counter-intuitive as I'd prefer the prefab to represent the initial state of the Ability and not have the altered state carry over between game sessions.

One solution I've considered but haven't tested is to create a series of dummy objects which have the prefab Ability behavior applied to them, and add these dummy objects to the card prefabs; then use these instances for the card abilities rather than the Ability prefabs. This seems like a very cumbersome solution, though, as I now have to go through each card and create these dummy game objects and apply the ability to each game object and then tie the game object back to the card... ugh.

Is there a way to get the prefab to just clone itself when referenced rather than have to go through all this work?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cirrusplay · Aug 11, 2017 at 04:29 PM

And but seconds after I write my question, I come up with a solution. I don't know if this is the best way to go about it, but it works. I'm still excited to see other people's solutions if they can find a better one, but my solution simply entails going through all of my Card abilities once the Card is initialized and cloning each Ability Game Object, then replacing the Card's ability with the new Game Object's Ability. This appears to create a clone of the Ability which is exactly what I need. Here's some code for others in the same pickle (code is contained in my Card.cs file and called from Start()):

 private void CloneAbilities()
 {
     if (this.abilities != null && this.abilities.Length > 0)
     {
         for (var i = 0; i < this.abilities.Length; i++)
         {
             var ability = this.abilities[i];
             if (ability != null)
             {
                 var go = GameObject.Instantiate(ability);
                 this.abilities[i] = go.GetComponent<Ability>();
             }
         }
     }
 }
Comment
Add comment · 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

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

83 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

Related Questions

Reference a Game Script in Prefab 0 Answers

OnTriggerEnter2D not working on sprite's prefab clones 0 Answers

Cant delete clones of prefab 1 Answer

References to scene objects in prefabs - Inspector vs. Find 1 Answer

Instantiate Clones Itself - Rather than Prefab 0 Answers


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