- Home /
Better way to do this? Instatiate or simply turn on and off?
Hi,
I have a simply question, regarding which would be the 'better way' to do this particular thing..
I have a very simply room with 4 paintings.
Initially they are blank.
Underneath the paintings, they have a button(labelled 1 - 4) as per each painting.
When you press a button, the corresponding painting is no longer blank but has a 2d picture. When pressing the button again, the corresponding painting goes blank.
eg;
b b b b 1 2 3 4
b= blank numbers=buttons
press 1, b becomes P (a painting with a picture) press 1 again, P becomes B(a painting blank again)
in doing so, a person can have 4 blank paintings, 4 pictured paintings or a mix.
Now my question is:
Is it better to instantiate a picture painting(2d object) and destroy it as per button commands OR is it better to simply put a boolean.(If true, picture is shown, if false pciture is hidden and thus you only see a blank background).
Paintings are in 2d.
I will likely have a lot of paintings etc so I was wondering would it be better to therefore instatiate or because it is 2d, it won't matter if they are all 'there' but hidden and I use the boolean true/false method.
Thanks in advance.
Not talking from experience, but the general idea that I usually seem to find is that it's always a bad idea to constantly instantiate and destroy objects during gamePlay. $$anonymous$$ost people usually seem to suggest that you should instantiate all objects at the start of a scene and either hide/show them or create some form of pooling for those objects assu$$anonymous$$g that you know how much of those objects you'll need.
I guess you could check this yourself if you have Unity Pro by creating a simple test scene and comparing the memory usage (and whatever else the profiler spits out) between a scene where you instantiate and destroy objects constantly VS a object pool.
You know (A) performance is irrelevant at this stage. (B) the only "correct, sensible" way to do it in this situation is using SetActive. (C) it is not necessary to test in the profiler exactly as you suspected Shadow, it is like 18 billion times slower to instantiate.
Answer by Fattie · Oct 03, 2013 at 07:48 AM
The answer is to simply turn it on and off - it's that simple.
Just use SetActive(true) and SetActive(false).
OK?
There are other strategies, like you will see people turning on and off the renderer. But really at your stage just use SetActive.
Hi, currently I am turning renderer on and off, since it's It's merely a bunch of 2d images.
And thankyou for notifying me to 'tick' an answer. I'll do so from now on.
edit: sorry to be a bother, but I do not see a 'tick' option?
Your answer
Follow this Question
Related Questions
Destroying Instantiated Obect Problem 1 Answer
Checking if object intersects? 1 Answer
Instantiate problem. 3 Answers
How to instantiate once when boolean changes 1 Answer
Instantiate rotation question. 1 Answer