- Home /
Setting up EventTrigger programmatically for GameObject throws NullReferenceException
Hi! There's a problem here. I have an array of dynamically added GameObjects. Everything works fine. The main Game Controller executes some other scripts. Then, when it would come to set up an EventTrigger for my game objects, when I call eventTrigger.delegates.Add(anEventTriggerEntry), it throws me NullReferenceException. Both the entry and the trigger is well set-up. I tried everything: placing the code elsewhere, applying it on other game objects. I also tried different solutions but none worked.
I tried the following solutions. And I mean, I used EXACTLY the same solutions: http://answers.unity3d.com/questions/781726/how-do-i-add-a-listener-to-onpointerenter-ugui.html http://answers.unity3d.com/questions/854251/how-do-you-add-an-ui-eventtrigger-by-script.html http://gamedev.stackexchange.com/questions/83027/unity-4-6-gui-dynamically-change-the-functions-called-by-gui-elements
I have a single instance Game Controller in which I have a method that would set up the triggers and is executed by an other instance script (which is called by the Game Controller first).
Using any of them, I get NullReferenceException at eventTrigger.delegates.Add(entry). I would be delighted to get help. Now I am having this problem for over 4 months, and I found no solution.
Have you debugged your code? You have to find what is null in that statemant. It could be "eventTrigger", "eventTrigger.delegates" or "entry".
Some times I have null pointers on unexpected places because I do some stuff on the Start method in 2 objects, one of them is supposed to call something from the other object, but since there's no control on what Start method is called first, I ended up calling a method on the second object before it's Start method.
$$anonymous$$aybe your "eventTrigger" variable is setup in some Start method and the code that calls the line "eventTrigger.delegates.Add(entry)" runs before the variable has been initilized.