Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by suzhai · Jun 13, 2016 at 06:02 AM · c#staticeventsystemdrag and drop

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);
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image corn · Jun 15, 2016 at 08:14 AM 0
Share

@suzhai When the NullRef occurs, which object is null ? WorkerController.Instance or WorkerController.Instance.hiredWorkers ?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges