I am trying to make a merge game, but I didn’t manage to understand the tutorials, so I tried my code:
// share code as code-formatted text here, please
First, on collision, I identify if the two colliding gameobjects are the same ones.
In order to destroy them both and Instantiate only ONE gameobject, I made a script that deals with the problem (I think) by generating a random value that will decide weather the “first” or the “second” gameobject will Instantiate the object.
However, when I test it, it is successful sometimes, and sometimes the gameobjects disappear and nothing is Instantiated.
Then, I tried with a
0.01 second break in order to enable the gameobject to have enough time to Instantiate, and with no lag.
I hope you can help me.
Is it possible that the instantiated object is also colliding with the first two objects thus causing a OnCollisionEnter2D check which causes it to disappear right after instantiation?
Try printing objects name with a
OnDestroy method to see how many objects dissapear. If it is more than 2 then it means it is not that its not instatiating. its just that it dissapears too fast so you can’t see it. You can give a unique name to instantiated object to see if it is really destroyed.
You can try disabling colliders on collision? Or changing the layer of first two objects to something like “check” layer and disable collisions between the normal layer and check layer.
These are just some random ideas that came to mind. It is possible that its not any of these but it is worth trying, probably, possibly, maybe. Do let me know if it works.
It seems like the gameobjects aren’t created sometimes, when I look in the console.
The layer didn’t change anything either.
I created a script that debugs when:
• An object is destroyed (for example: “Triangle destroyed”)
• An object is Instantiated (for example: “Square merged”)
It seems like sometimes objects are skipped from being instantiated (It happened more times but I didn’t screenshot the whole console).
Also let me know if you have any code that could work too without this logic.
It is weird that it returns a merge message but doesn’t instantiate the object. Is it possible that the prefab gameobject is null ?
Maybe add a debug in the
PolygonMerge collision enter
Debug.LogWarning(collision.transform.name) to see the names of the objects that collide. It doesnt have to be a warning, i find it easier on the eyes. change the
> code to else if as well.
Other than these, maybe store the instantiated object in a local variable and enable it by code?
For debug purposes rather than destroying the objects. Just disable them for now so you can see if there is an object(ex. square) instantiated as disabled. Also don’t forget to change the code where you send a message
I don’t have any more debug ideas for now.
Are there any other scripts that may cause problem in your script, maybe we are looking at the wrong place?
I might be wrong, but if you watch the video and the console, the two triangles merge into a square at first, at 09.30.55, which is normal. The next messages are triangle destroyed twice, at 9.30.58, so they are destroyed successfully, but nothing is instantiated.
I conclude that the instantiate isn’t detected because it is not made, and it is not a null gameobject.
Also thanks for replying me when you see my issues.
I made a few tests and I found out something; the object is istantiated only if the random of the object who does the action first is greater than the other one.
Then I concluded that (and i might be wrong) it is probably because the object who executes the script is deleted (if his random is less than the other’s random), so I tried to put the script that deals with the instantiate and destroy part in a void that i call with the same conditions, in order to be sure the action continues even if the collision is not active anymore, but now I can’t use the “collision.gameobject…” because it is not in the OnCollisionEnter void. How can I deal with that?
That sounded like a brilliant idea to me, but I don’t know why, the inspector tells me that i didn’t set the gameobject collision to a value. I don’t understand why does this code not work:
// share your code as code-formatted text instead, please
Also don’t feel bad for not replying. Hope that stuff is better now.