Question by
Nattress · Aug 02, 2016 at 01:05 PM ·
error messagebeginner
Reference to Null object Help beginner question
NullReferenceException: Object reference not set to an instance of an object PlayerController.Update () (at Assets/Scripts/PlayerController.cs:23) I got stuck while trying to follow this tutorial https://www.youtube.com/watch?v=poeXGuQ7eUo here is code: using UnityEngine; using System.Collections;
public enum SwipeDirection
{
None = 0,
Left = 1,
Right = 2,
Up = 4,
Down = 8,
}
public class SwipeManager : MonoBehaviour {
private static SwipeManager instance;
public static SwipeManager Instance{get {return instance;}}
public SwipeDirection Direction{set; get;}
private Vector3 touchPosition;
private float swipeResistanceX = 50.0f;
private float swipeResistanceY = 100.0f;
private void start(){
instance = this;
}
private void Update()
{
Direction = SwipeDirection.None;
if (Input.GetMouseButtonDown (0)) {
touchPosition = Input.mousePosition;
}
if (Input.GetMouseButtonUp (0)) {
Vector2 deltaSwipe = touchPosition - Input.mousePosition;
if (Mathf.Abs (deltaSwipe.x) > swipeResistanceX) {
Direction |= (deltaSwipe.x < 0) ? SwipeDirection.Right : SwipeDirection.Left;
}
if (Mathf.Abs (deltaSwipe.y) > swipeResistanceY) {
Direction |= (deltaSwipe.y < 0) ? SwipeDirection.Up : SwipeDirection.Down;
}
}
}
public bool IsSwiping(SwipeDirection dir)
{
return (Direction & dir) == dir;
}
}
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float minSwipeDistY;
public float minSwipeDistX;
private Vector2 startPos;
Animator animator;
// Use this for initialization
void Start () {
animator = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
if (animator.GetInteger ("Action") != 0){
animator.SetInteger ("Action", 0);
}
if (SwipeManager.Instance.IsSwiping(SwipeDirection.Down)){
animator.SetInteger ("Action", -1);
}
}
}
Both scripts are applied to my player object if that helps
Comment
Answer by gjf · Aug 01, 2016 at 05:42 PM
unity will not call start()
but will call Start()
- you need to pay special attention to the case of unity called functions.
make that change and see if the problem still occurs.