Error creating joystick.NullReferenceException: Object reference not set to an instance of an object. How to fix?

Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
The script movement of the joystick. 2 the Central object(moving)(joystick) and static (joystickBG) NullReferenceException: Object reference not set to an instance of an object.
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class MobileController : MonoBehaviour,IDragHandler,IPointerUpHandler,IPointerDownHandler { private Image joystickBG; [SerializeField] private Image joystick; private Vector2 inputVector; private void start () { joystickBG = GetComponent(); joystick = transform.GetChild(0).GetComponent(); } public virtual void OnPointerDown(PointerEventData ped) { OnDrag (ped); } public virtual void OnPointerUp(PointerEventData ped) { print ("PointerUP"); inputVector =; joystick.rectTransform.anchoredPosition =; } public virtual void OnDrag(PointerEventData ped) { Vector2 pos; if (RectTransformUtility.ScreenPointToLocalPointInRectangle (joystickBG.rectTransform, ped.position, ped.pressEventCamera, out pos)) { pos.x = (pos.x / joystickBG.rectTransform.sizeDelta.x); pos.y = (pos.y / joystickBG.rectTransform.sizeDelta.y); inputVector = new Vector2 (pos.x * 2 - 1, pos.y * 2 - 1); inputVector = (inputVector.magnitude > 1.0 f) ? inputVector.normalized : inputVector; joystick.rectTransform.anchoredPosition = new Vector2(inputVector.x * (joystickBG.rectTransform.sizeDelta.x / 2), inputVector.y * (joystickBG.rectTransform.sizeDelta.y / 2)); } } }
by | 143 views

1 Answer

0 like 0 dislike
In the manual there is even a special article about this exception. You should start with double click on the error in the console, you will need to open the editor with the selected line which fault occurred. In most cases this is enough, and you can immediately understand what the object was null, and why. If you just do not to understand, or connect a debugger and see what's not working, or the weight wrong all demogame and read the console.
110,608 questions
257,187 answers
40,796 users