I want to enable and disable instead of instantiating and destroying everytime. It's pooling system that i seek.
Asked
Active
Viewed 112 times
0
-
Which line is causing the error? – Gunnar B. Jul 02 '16 at 11:58
-
Not a line. it's caused in unity editor and its not displaying in the script – David Jul 02 '16 at 11:59
-
As you can see in the obstacle script i had //manager.SpawnNewObstacle(transform.position , 2); but i changed it to manager.SpawnNewObstacle(); And im doing the transform.position in the script manager – David Jul 02 '16 at 12:01
-
The behavior that i want is happeing but the errors shows up when i destroy and respawn 3 obstacles. so when i destroy 1 obstacle another random obstacle spawns after 2 seconds, same thing for obstacle 2 but obstacle 3 starts giving index our of range – David Jul 02 '16 at 12:04
-
Ah, ok, that means that there are no positions left I'd say. Add `if(freePositions.Count <= 0) return;` at the beginning of `Spawn`. – Gunnar B. Jul 02 '16 at 12:06
-
This removes the error but it doesn't fix my problem :( ye there are not points left to spawn but i need the points because i want to keep spawning in the available points – David Jul 02 '16 at 12:11
-
There will be free points again when you destroy an object. This should mean that there will allways be all points occupied (with your delay) . – Gunnar B. Jul 02 '16 at 12:16
-
Yes that should happen but for some reason when i destroy the 3rd obstacle the lists is empty and it doesent access or know about the free points on the scene... so it doesn't spawn anything – David Jul 02 '16 at 12:17
-
Hmm, maybe reversing the for-loop in the coroutine does something for this (making it start at that last and going `i--`). – Gunnar B. Jul 02 '16 at 12:19
-
For debugging you can also expose the two lists to the inspector to check them on runtime. – Gunnar B. Jul 02 '16 at 12:23
-
can u check the updates? i added a picture – David Jul 02 '16 at 12:25
-
Ah, not to the console, to the inspector ;) Make them public. – Gunnar B. Jul 02 '16 at 12:30
-
ohh okay this is what happeing.. When i start the game i have 2 free position and 6 taken. so when i destroy 1 obstacle and respawn it makes 7 taken and only 1 free and when i destroy the 2nd obstacle it gices me 0 free and 8 taken. so thats why its not respawning again because there is no free position anymore.. It's not updating the free positions when i destroy .. – David Jul 02 '16 at 12:33
-
Add a Debug.Log to the if of the coroutine ti check if a position gets free. If not this might be because of float imprecision (something like 5 gets 4.998 or so). – Gunnar B. Jul 02 '16 at 12:38
-
it doesn't get free the debug is not being triggered it's not going through the if statement? this is weird.. – David Jul 02 '16 at 12:42
-
Yes, I think the reason is that you will have some imprecision on the positions. Say you place the spawn point at `(5, 5, 5)`. The spawned obstacle might end up at `(5, 5, 4.998)` and the comparison fails. Maybe have a look at that on runtime in the inspector or add a debug before the if to see the values. – Gunnar B. Jul 02 '16 at 12:56
-
It would be better to compare transforms because that would not fail, but you can't pass the transform of the destroyed obstacle I think. – Gunnar B. Jul 02 '16 at 12:57
-
to be honest im confused now ^^" – David Jul 02 '16 at 13:01
-
i know this is bad thing to say but is it possible to send you the project so you can understand the error i cant fully explain what is happeing over here =/ – David Jul 02 '16 at 13:05
-
Chuck it in your question, I'll have a look. – Gunnar B. Jul 02 '16 at 13:06
-
is it possbile to put a video? – David Jul 02 '16 at 13:08
-
If you upload it to youtube or something. – Gunnar B. Jul 02 '16 at 13:13
-
https://youtu.be/qqqPVpaP8K4 – David Jul 02 '16 at 13:17
-
That doesn't really help because I don't see the positions of the object. Add a `Debug.Log(occupiedPositions[i].position + " " + freePos)` before the if in the coroutine. I suspect that you will have one pair per destroy that is very close, but not exactly the same. – Gunnar B. Jul 02 '16 at 13:24
-
sorry, please check the updates added a image – David Jul 02 '16 at 13:28
-
Hmm, the right position is always the same and it seems like one is in world space and one in local space, so there is a missmatch there. – Gunnar B. Jul 02 '16 at 13:44
-
how come each one of them is in different space? – David Jul 02 '16 at 13:48
-
I didn't notice you guys are talking about "pooling". **You absolutely MUST NOT POOL in Unity today** http://stackoverflow.com/a/37865231/294884 This is an astounding time saver - no more pooling. It's the second-best feature of Unity after the new UI system. – Fattie Jul 02 '16 at 14:35
-
@JoeBlow actually not object pooling, but "position pooling". He has a couple spawn points (I assume empty gameobjects), that are placed around and he takes a free point to spawn an object (on instantiation and after on existing got destroyed). Somehow with the above code he got a missmatch in there. The debug for the position test compares a a position to always the same position (and the seems to be a missmatch of world and local space or the comparison of wrong objects or something). – Gunnar B. Jul 02 '16 at 14:56
-
OK, well that's not called pooling. I have given him the perfect code to do exactly that! – Fattie Jul 02 '16 at 15:05
-
OK so I realised what the fundamental problem was. OP was conceptually trying to keep a "list of occupied positions". You can't do that, you can't use models, you have to look at reality. it is maybe the most common thing in gaming: "result = random", then "while not suitable, result = random" (in this case "not suitable" == "occupied") – Fattie Jul 02 '16 at 15:22
1 Answers
1
instead of destroying it i want to disable it and instead of instantiating it again i want to enable it in a random position and type
You can't do that.
But the solution is incredibly simple. When you destroy it, call to the manager for a new one.
Exactly as you do here:
public void OnMouseDown()
{
manager.SpawnNewObstacle(transform.position);
Destroy(gameObject);
}
You're done!
Let's say you want (for example) the same type to spawn. Or, one green produces three gold, for example. Just do this
public void OnMouseDown()
{
if (myType == .Green)
manager.SpawnThreeGold(transform.position);
if (myType == .Gold)
manager.SpawnOneBlack(transform.position);
Destroy(gameObject);
}
.. or whatever the case may be. It's that easy.

Fattie
- 27,874
- 70
- 431
- 719
-
thanks. but to be honest i dont fully understand your code. plus i need to change all of this to pooling system so i dont think a invoke will work with me. – David Jul 02 '16 at 14:23
-
Good news. You absolutely DO NOT NEED a pooling system. You haven't needed one for 5 years in Unity. We wrote one of the original pooling systems back in the day. Fortunately not necessary now. Can you explain how many objects you have, what is the max count, and how many are created per second. – Fattie Jul 02 '16 at 14:27
-
Hello Thank you very much for your hard work and time to explain this to me but kindly can we start the explanation from the beginning im really confused with the amount of explanation and spawn points that i currently have i would really appreciate it and thank you again. sorry for being a newbie @joe Blow – David Jul 02 '16 at 18:02
-
i need to pool.. because im going to add a networking features.. i dont want to keep instantiating and destroying all the time i just want to simply enable and disable .. – David Jul 02 '16 at 18:14
-
-
Again thank you for much for your hard explanation! but please dont leave me now xD – David Jul 02 '16 at 18:34
-
got to many questions and errors please when you are free comment :D! really really i appreciate this.. you are simply.. awesome even if i dont have what i want you are really awesome :D! – David Jul 02 '16 at 19:48
-
-
-
In the CreateObstacle() there is n.name what "n" is exactly and why are you adding name? – David Jul 03 '16 at 15:50
-
no more questions i just need to finish this already! i've been working on it the past 3 days i wanna be done! – David Jul 03 '16 at 15:50
-
because nu.name line is giving me errors. it saying there is no nu.Length – David Jul 03 '16 at 15:53
-
Bro stuff are complicated here... i just went back to my old code and the behavior i want is there its just missing the stuff getting enabled and disabled instead of keeping instantiating and destroying and it's also missing the delay spawn. im thinking of removing the delay because it caused a lot of errors and problems for me. simply thats everything i need.. – David Jul 03 '16 at 16:06
-
-
just a simple error, nothing to worry about! and yeah you MUST set the name – Fattie Jul 03 '16 at 16:22
-
how does this script gives me the same behavior in the video? 1: only 1 type spawns , 2:they dont disable and enable after delay they just keep on invoking. – David Jul 03 '16 at 16:55
-
invoke will keep instantiating objects. i want to instantiate only ONCE and when i destroy objects i want to disable them and when i reinstantiate i enable them again with new positions – David Jul 03 '16 at 17:04
-
do you want one item (say "x") to disappear ON ITS OWN after how many seconds? – Fattie Jul 03 '16 at 19:59
-
i want when i destroy obstacle to enable another one. so here's what i excatly need: instantaite x amount of obstacles then when i want to destroy them i disable them and reenable them when as respawn – David Jul 03 '16 at 20:41
-
-
I cant think f way to have my same behavior but instead of destroying and instantiating i want to do it pooling – David Jul 03 '16 at 20:48
-
Yes :D i already have the points for it to spawn. I have a public spawnpoints where my obstacles showed instantiate at – David Jul 03 '16 at 20:49
-
I have a percentage system. Where it checks the percentage of the obstacle to instantiate it. my normal obstacle is 80% and my special one is 20% therefore the normal obstacle will show more often. – David Jul 03 '16 at 20:51
-
you say: **"but instead of destroying and instantiating i want to do it pooling"** the answer is ***you cannot do that in Unity***. What you are doing now is perfect. Is it working well ? – Fattie Jul 03 '16 at 20:52
-
its working very good. but my team doesnt want me to keep instantiating and destroying they want one time instantiate then enable and disable. – David Jul 03 '16 at 20:53
-
how i cant do pooling in unity? i have seen a lot of tutorials doing it i just cant figure out my logic with it – David Jul 03 '16 at 20:55
-
it looks ABSOLUTELY FANTASTIC. can you expain the problem? you seem to be forgetting to check the position ***of the one yuo just destroyed***. Sometimes you get the same position. – Fattie Jul 03 '16 at 20:55
-
Try to understand that LOTS AND LOTS OF EXAMPLE CODE for Unity which you see around the net, is, totally out of date. – Fattie Jul 03 '16 at 20:57
-
i really do understand and i dont even want pooling! im done with this system its just the team wants that i dont know what to do with them! – David Jul 03 '16 at 20:58
-
a good idea is JUST TELL THEM YOU ADDED "POOLING" ................ ***BECAUSE POOLING IS NOW BUILT-IN TO HOW "INSTANTIATE" WORKS IN UNITY!!!!!!!!*** – Fattie Jul 03 '16 at 20:59
-
I know bro i cant thank you enough :) sorry for pissing you off man .... really thank you – David Jul 03 '16 at 20:59
-
another idea is this dude. if you use prefabs, ***UNITY ADDED AUTO POOLING TO PREFABS***. so if you change to using a prefab, you can honestly say you changed to UNITY POOLING – Fattie Jul 03 '16 at 21:00