(Solved)How can I pass the script between GameObjects

Hi there

I am making object pool to optimizing my project. My project has involved object appearence change and I planned to using object pool to recycle the old appearence while the new appearence is generated. However, I found it is tricky to pass the scripts attached to the old appearence into the new one.

Currently, I had two solutions. One is to create an empty object as the parent for both appearence and attach the scripts into the parent object. The other is I just pass the param between two object manually.

However, I am not satisfied with the previous two solutions. Does anyone has better ideas ?

Thanks。

Sounds like a case where you just need to encapsulate the relevant information into it’s own class/struct and pass that through.

Are you really sure you need to do this?

Before you wreck your project, just learn what you’re doing:

The costs and issues associated with object pooling / pools:

https://discussions.unity.com/t/892797/10

https://discussions.unity.com/t/833104/2

1 Like

Yes, that is my solution two, passing param manually. While I wish to found a way that can make this process as concise as GameObject.AddComponent<>(). Any clue?

As spiney199 has pointed out you might be able to encapsulate the data into an object but the answer requires a whole lot more information. I suspect there are lots of alternatives dependent upon what the changes are. Is this something a factory class could build and hand back to you?

1 Like

Right, but did you understand the ‘encapsulate’ part? Once you have everything neatly wrapped into one object you can easily do something like .ApplyAppearance(AppearanceData data);.

This of course will likely involve a decent refactor to represent this information in a more plain C# object manner.

That said, don’t optimise if you don’t have a performance issue.

2 Likes

Really greatful for your answer. It gives me a clearer view of the object pool usage. And yes, I will still using object pool in this part cause I need to change the apprearance as frequent as creating bullet and the info related to those apprearance is limited.

Yes, I understand what’s you mean. I think I was over pursuited of code simplicity.