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 jchuah · Jan 16, 2013 at 04:18 PM · prefabaddcomponentpool

AddComponent in script modifying Prefab Assets?

I've got some weird behavior going on in my code and I'd like to know what's causing it. Here's the setup:

There's a Brick prefab. There's a Pool prefab that's supposed to be an instance factory/pool. This Pool prefab has a monobehaviour with an empty GameObject variable for the GameObject that it's supposed to replicate.

I create a scene instance of the Pool in the scene and call it BrickPool. The BrickPool scene instance has it's Prefab variable set to the Brick Prefab from my Assets.

At runtime, the pool uses AddComponent to append a Poolable Component (with a Return method) onto whatever Prefab it's supposed to pool, then instantiates 20 of that Prefab in the scene.

Here's the weird behavior: Each time I run the program (not each time a Brick is pooled), a duplicate Poolable component gets added to my Brick class. If I run the program 4 times, Poolable gets added as a component to the Brick Prefab asset and remains there, even if the game isn't running.

What am I doing wrong?

Here's the relevant stuff from the Pool class:

 public var Prefab : GameObject;
 public var Size = 20;
 private var _pool : GameObject[];
     
 function Start() {
     _pool = new GameObject[Size];
     
     Prefab.AddComponent(Poolable);    
     
     for (var i = 0; i < Size; i++) {
         var go = Instantiate(Prefab, Vector3.zero, Quaternion.identity);        
         _pool[i] = go;
     }
 }

I'm not sure how to illustrate the Unity editor assets and scene instances other than to take copious screenshots, but I'll do so if it provides the necessary information to describe what's happening.

Comment
Add comment · Show 3
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 AndyMartin458 · Jan 16, 2013 at 06:06 PM 0
Share

It sounds like adding a component to a prefab changes that prefab permanently. Why don't you just try removing the component in OnApplicationQuit(){}. That would get rid of it any time you stop your project.

avatar image jchuah AndyMartin458 · Jan 16, 2013 at 07:08 PM 0
Share

It's ultimately not going to impact development. It's more of a question I'm asking from the perspective of being a Unity $$anonymous$$cher for middle school students, and also from a runtime memory/efficiency perspective.

If I package the application, it's probably not going to be re-writing the Prefab inside the package. If it modifies it in Unity Editor, I don't have to save any changes made. (The changes don't appear when I reload the scene.) I could also stick in code that checks for the component before adding it.

avatar image AndyMartin458 AndyMartin458 · Jan 16, 2013 at 09:41 PM 0
Share

I don't see why you want to add the component during run time in the first place.

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

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

Related Questions

Adding prefab ParticleSystem to a GameObject? 0 Answers

Using a pool of objects that will be spawned ONCAMERA and removed OFFCAMERA instead of INSTANTIATE & DESTROY (iOS) 1 Answer

prefab as AddComponent? 1 Answer

pooled prefab missing child 1 Answer

(Similar Prefabs) vs (Single Prefab + AddComponent) 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