- Home /
Error when doing Penelope Tutorial. Need Help
I just finished the joystick script and everything is fine but when I attach the Joystick script to the right joystick I get a "NullReferenceException" as an error. The script is perfect when it's on the left pad but when I add it to the right pad I get that error.
When I double click on the error it highlights this line of code.
gui.pixelInset = defaultRect;
I just needs someone to explain what that error means.
Answer by Cap · Jun 03, 2010 at 11:52 AM
Could there be a second script or a GUI component you need to attach as well? Check both joysticks in the inspector and make sure they have all the same components.
So far I've made the Joystick Script and that's all and I've had it attached to the left Joystick the whole time. Once I finish writing the Joystick Script, and everything is fine at that point, I go ahead and attach the script to the Right Joystick and then I get the error. When I test run it the Joystick still moves as it was but the Right doesn't move at all even though there made the same just with different names.
Well, from the error it looks like the script is trying to reference something and can't find it. defaultRect must be a custom variable, so look for it somewhere in your first joystick and its children (if any) and attach the same component/object to the second one if you find it. There's also a chance the second joystick needs something like a GUILayer component or one of the other GUI components. In any case, something which exists in/for the first joystick is missing for the second.
This answer was very helpful but it turns out that it was working but it only moves one texture for some reason ins$$anonymous$$d of both. Basically the joystick texture was in different spots but the center of the rectangle (defaultRect) we were referencing to was in the same spot for both the joysticks. We can see in the inspector though that were moving both the joysticks but only one Joystick has it's texture move. After doing some tests I feel as If unity randomly picks which texture moves whenever I close and re-open Unity.
Ah, well then just duplicate the GUI whateveritis and alter the script for the second joystick so it uses the new one. Adding a new variable outside your functions in that script file and setting its type to whichever GUI component you're using will let you drag the first texture to the first joystick's inspector and the second texture to the second.