- Home /
 
Drag and Drop PlayerPrefs
So I have developed drag and drop simple game that I follow from youtube "Unity UI Drag and Drop Tutorial" by BoredMormon Games. Below are my codes.
I was wondering, how to save the correct answer to playerPrefs?
Slot.cs
 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class Slot : MonoBehaviour, IDropHandler {
     
     public GameObject item {
         get {
             if (transform.childCount > 0) {
                 return transform.GetChild (0).gameObject;            
             }
             return null;
         }
     }
 
     #region IDropHandler implementation
     public void OnDrop (PointerEventData eventData)
     {
         if (!item) {
             DragHandler.itemBeingDragged.transform.SetParent (transform);
         }
 
         if (DragHandler.itemBeingDragged.tag == transform.tag) {
             Debug.Log ("correct");
         }
     }
     #endregion  
 }
 
               DragHandle.cs
 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
 
     public static GameObject itemBeingDragged;
     Vector3 startPosition;
     Transform startParent;
     public Canvas myCanvas;
 
     #region IBeginDragHandler implementation
 
     public void OnBeginDrag (PointerEventData eventData)
     {
         itemBeingDragged = gameObject;
         startPosition = transform.position;
         startParent = transform.parent;
         GetComponent <CanvasGroup> ().blocksRaycasts= false;
     }
     #endregion
     
     #region IDragHandler implementation
     public void OnDrag (PointerEventData eventData)
     {
         transform.position = Input.mousePosition;
 
         Vector2 pos;
         RectTransformUtility.ScreenPointToLocalPointInRectangle(myCanvas.transform as RectTransform, Input.mousePosition, myCanvas.worldCamera, out pos);
         transform.position = myCanvas.transform.TransformPoint(pos);
     }
     #endregion
 
     #region IEndDragHandler implementation
 
     public void OnEndDrag (PointerEventData eventData)
     {
         itemBeingDragged = null;
         GetComponent <CanvasGroup> ().blocksRaycasts = true;
         if (transform.parent == startParent) {
             transform.position = startPosition;
         }
     }
 
     #endregion
 }
 
 
              Answer by FortisVenaliter · Jan 27, 2016 at 06:51 PM
PlayerPrefs has Set and Get methods. You call them when you want to save or load. This is well documented.
Maybe I'm not understanding the question?
Answer by aan_comel · Feb 19, 2016 at 01:48 PM
Never mind, I think this is the correct answer. Just add a few of these lines.
slot.cs
 public class Slot : MonoBehaviour, IDropHandler {
     
     public static void Save() {
         PlayerPrefs.SetInt ("NUM OF TRUE ANSWER", 1);
         PlayerPrefs.Save();    
     }
 .
 .
 .
         if (DragHandler.itemBeingDragged.tag == transform.tag) {
             PlayerPrefs.GetInt("NUM OF TRUE ANSWER");
             Debug.Log ("correct");
             Save ();
         }    
     }
 }
 
               DragHandler.cs
 public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
 
     public static GameObject itemBeingDragged { get; set; }
 
              Your answer
 
             Follow this Question
Related Questions
Save values on different objets with same script? 1 Answer
PlayerPrefs don't work 2 Answers
Adding Value to Already Saved Values 1 Answer
How to save different values with same script? 1 Answer
Drag and Drop on RTS camera 1 Answer