Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Hakej37 · Nov 20, 2020 at 01:58 PM · prefabunity 2dscripting beginner

What is the correct way to use prefab in my example?

So I've got a plant GameObject which has stuff like how long does it takes to grow, how does it look when growing and when grown (sprites) etc., as well as scripts to make it grow.


Until now, whenever I needed a new plant I just duplicated the prefab and changed things accordingly, but now I wanted to add a particle effect whenever a plant grew, and it made me rethink if I even use prefabs in the correct way, because I could add a particle system via script, but if that would be a single prefab I could just add it via inspector instead.


Is there a way that I can use a single prefab like some kind of class? I thought about one prefab called Plant, but I have no idea what is the best way to add new plants and set their values (as I mentioned, I have Sprites of the plants and it feels more comfortable to set it via inspector, not via code). I want it to be possible to have multiple plants of multiple types (for example I can have 3 carrots that behave exactly the same and 2 potatoes that behave like a potato, but not like a carrot).

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by alexxUID · Nov 20, 2020 at 02:28 PM

You should use prefab variants to do what you are looking for.

Start with a base prefab, that holds all the stuff like audio, base scripts like growing and so on. Here you place your "grow" particle system as well. Now you can create prefab variats of that base prefab that represent your different plants. For each plant a new one - if they have different functionality.

Like this you can still edit the base prefab and e.g. change the particle effect for growing and all your variants automatically get the changed particles without you having to change them all.

To be more precise:

  • create a base prefab "plant" of an empty gameObject

  • add the base scripts to this prefab

  • add particle effect to that prefab

  • now create a prefab variant off of that base prefab called carrot

  • add all components you need to implement the carrot on that new prefab variant like the sprites of the carrot and the special behaviour scripts it can have.

Comment
Add comment · Show 1 · 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 Hakej37 · Nov 20, 2020 at 02:47 PM 0
Share

This definitely seems to be something I'm looking for! Thank you very much, it opens so many possibilities now.

avatar image
0

Answer by Llama_w_2Ls · Nov 20, 2020 at 02:12 PM

You can have a class that contains instances of itself, that you can then assign in the inspector, and display accordingly. For example:

     public class Plant
     {
         // These can all be assigned in the inspector
         // as long as you have a script containing this class
         // and you only need one prefab that can have
         // different assigned stages
 
         public Plant GrowingStage1; 
         public Plant GrowingStage2;
         public Plant GrowingStage3;
         public Plant Grown;
 
         public float TimeTakenToGrow = 60;
 
         //...
     }

@Hakej37 Is this what you wanted?

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

197 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 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

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. 1 Answer

Unity 2D movement and rotate sprite to the direction it is moving in 1 Answer

Changing transform.localScale of a prefab in the script not working. Help pls! 1 Answer

Handling friction in scripts for a 2D platformer 1 Answer

Can I create Scene via script? 2 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