- Home /
Is it possible to call a static class while dragging?
Hello and thanks in advance for the help.
I'm getting an error when referencing a dictionary in a static C# class from within a script that handles dragging.
NullReferenceException: Object reference not set to an instance of an object
Summary: The player drags a game object (a worker) and drops it on another (the job location) which kicks off an action. When dragging, I'm creating an icon and dragging that along with the pointer. In order to set the icon to the proper worker, I want to reference the data object for the worker via a dictionary where I keep them in another class.
The dictionary where I keep the worker list is accessible from everywhere else in the application, just not the draggable script or anything is calls.
My code is essentially: Worker Controller
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class WorkerController {
public static WorkerController Instance;
// Dictionary to hold hired workers
public Dictionary<string, Worker> hiredWorkers;
public WorkerController () {
Instance = this;
// create the dictionaries
hiredWorkers = new Dictionary<string, Worker> ();
}
// methods below for generating random workers using a json file and worker class
}
Interface Controller:
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InterfaceController : MonoBehaviour {
void Start () {
// junk that loads up all the sprites
}
public Sprite getDragSprite(string workerName) {
// get the worker object
// NEXT LINE FAILS - which is weird bacause it works everywhere else...
Worker wObj = WorkerController.Instance.hiredWorkers[workerName];
Sprite headSprite = new Sprite();
headSprite = spritesDict["headSm"];
// if(wObj.gender == true) {
// headSprite = spritesDict["headSmF"];
// } else {
// headSprite = spritesDict["headSm"];
// }
return headSprite;
}
}
Draggable Script
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections.Generic;
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public GameObject dragImageToCopy; private GameObject dragImage;
GameObject _go;
InterfaceController _goic;
void Start() {
//get the interface controller;
_go = GameObject.Find("Interface");
_goic = _go.GetComponent<InterfaceController>();
}
// WHEN DRAGGING CREATE AN ICON AT THE POINTER
public void OnBeginDrag(PointerEventData eventData) {
//get the worker name from the game object eventData
string _wName = eventData.pointerDrag.ToString();
Debug.Log("_wName: " + _wName);
//THE ABOVE DEBUG CONFIRMS THE STRING IS CORRECT
Worker wObj = WorkerController.Instance.hiredWorkers[_wName];
Debug.Log ("begin drag..." + eventData.pointerDrag);
dragImage = new GameObject ();
dragImage.AddComponent<Image> ();
//get the drag sprite from the InterfaceController
dragImage.transform.GetComponent<Image>().sprite = _goic.getDragSprite(eventData.pointerDrag.ToString());
dragImage.AddComponent<CanvasGroup> ();
dragImage.GetComponent<CanvasGroup> ().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData) {
dragImage.transform.position = eventData.position;
}
public void OnEndDrag(PointerEventData eventData){
Destroy(dragImage);
}
}
@suzhai When the NullRef occurs, which object is null ? WorkerController.Instance or WorkerController.Instance.hiredWorkers ?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to send mouse events directly to the canvas in world pos 0 Answers
C# Going Static or Going OOP 3 Answers
Can't Encode MD5 2 Answers