- Home /
Event System not working with dynamically instantiated objects
Hello all, the problem is the following: (mobile game) I have buttons on the screen that call linked functions in the player object movement script as usual, but they only work if the player object is already in the scene, when i try to instantiate it dynamically (in cases when the player dies it is destroyed and then instantiated again as a clone of the player prefab), the buttons does not works as expected. No error is displayed, they simply does not respond. I know i could find workarounds, even using the legacy GUI, but i really would like to know if there are a straight solution or maybe this problem is solved in the newest unity version, i am using Unity 5.1.2f1. Thanks in advance for any reply.
Is this still a thing? I seem to have the same issue
I think that this behavior is quite normal. The buttons have a reference to a specific instance of the Player GameObject, together with its components e.g. the movement script that contains the linked functions. When that player instance is destroyed, the button references become null.
So, after you instantiate the new player instance, you need to programmatically assign the new instance of the player movement script containing the functions, to the buttons.
Or, better yet, have a class that will manage only the GUI and it will interact with the other classes.
@RealWorld, please consider a new question ins$$anonymous$$d of waking up a 2 year old question.
I was considering an answer, but then realized I'd be applying an answer to a question from 2016 when that user hasn't been on the forum since 2016.
I'm converting this to a comment since it is not an answer. Also, please abstain from answering old posts where the OP hasn't even been active for years.