- Home /
How can I save the value that C1,C2,C3,C4 and C5 get inside the if condition and use it later to display it in a text?
Right now the Debug log shows me the value of C1 when I've dropped something in Container 1. But as soon as I drop something in Container 2, C2 gets a value but C1 is now null.
It represents that each eventData.pointerDrag.name is a different letter ( P, Z, A, I, A). I want to drag and drop each letter to a different container to form the word PIZZA. And I want the resultattxt to show me the word my containers are making, so when I drop the letter P it will show me P, then PI,..., and at the end PIZZA. But right now my variable A just show the last letter I've dropped.
Here is my code:
DropZone:
using UnityEngine; using System.Collections; using UnityEngine.EventSystems; using UnityEngine.UI;
public class DropZone : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler { private string C1; private string C2; private string C3; private string C4; private string C5; public Text resultattxt; public string A;
public void OnPointerEnter(PointerEventData eventData) {
if (eventData.pointerDrag == null)
return;
Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
if (d != null)
{
d.placeHolderParent = this.transform;
}
}
public void OnPointerExit(PointerEventData eventData) {
if (eventData.pointerDrag == null)
return;
Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
if (d != null && d.placeHolderParent == this.transform)
{
d.placeHolderParent = d.parentToReturnTo;
}
}
public void OnDrop(PointerEventData eventData) {
Debug.Log(eventData.pointerDrag.name + " dropped on " + gameObject.name);
Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
if (d != null) {
d.parentToReturnTo = this.transform;
}
if (gameObject.name == ("Container (1)"))
{
C1 = eventData.pointerDrag.name;
Debug.Log("C1="+C1);
}
if (gameObject.name == ("Container (2)"))
{
C2 = eventData.pointerDrag.name;
Debug.Log("C2="+C2);
Debug.Log("C1=" + C1);
}
if (gameObject.name == ("Container (3)"))
{
C3 = eventData.pointerDrag.name;
Debug.Log("C3="+C3);
}
if (gameObject.name == ("Container (4)"))
{
C4 = eventData.pointerDrag.name;
Debug.Log("C4="+C4);
}
if (gameObject.name == ("Container (5)"))
{
C5 = eventData.pointerDrag.name;
Debug.Log("C5="+C5);
}
A = C1 + C2+C3+C4+C5;
Debug.Log(A );
Start();
} public void Start() { resultattxt.text = A; } }
Draggable:
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections;
public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
[HideInInspector]
public Transform parentToReturnTo = null;
[HideInInspector]
public Transform placeHolderParent = null;
private GameObject placeHolder = null;
public void OnBeginDrag(PointerEventData eventData) {
placeHolder = new GameObject();
placeHolder.transform.SetParent( this.transform.parent );
LayoutElement le = placeHolder.AddComponent<LayoutElement>();
le.preferredWidth = this.GetComponent<LayoutElement>().preferredWidth;
le.preferredHeight = this.GetComponent<LayoutElement>().preferredHeight;
le.flexibleWidth = 0;
le.flexibleHeight = 0;
placeHolder.transform.SetSiblingIndex(this.transform.GetSiblingIndex() );
parentToReturnTo = this.transform.parent;
placeHolderParent = parentToReturnTo;
this.transform.SetParent(this.transform.parent.parent);
this.GetComponent<CanvasGroup>().blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData) {
this.transform.position = eventData.position;
if (placeHolder.transform.parent != placeHolderParent)
{
placeHolder.transform.SetParent(placeHolderParent);
}
int newSiblingIndex = placeHolderParent.childCount;
for (int i = 0; i < placeHolderParent.childCount; i++)
{
if (this.transform.position.x < placeHolderParent.GetChild(i).position.x)
{
newSiblingIndex = i;
if (placeHolder.transform.GetSiblingIndex() < newSiblingIndex)
newSiblingIndex--;
break;
}
}
placeHolder.transform.SetSiblingIndex(newSiblingIndex);
}
public void OnEndDrag(PointerEventData eventData) {
this.transform.SetParent(parentToReturnTo);
this.transform.SetSiblingIndex(placeHolder.transform.GetSiblingIndex());
this.GetComponent<CanvasGroup>().blocksRaycasts = true;
Destroy(placeHolder);
}
}
@annarmengou94 The problem is in the way you are using:
if (gameObject.name == ("Container (2)"))
{
C2 = eventData.pointerDrag.name;
Debug.Log("C2="+C2);
Debug.Log("C1=" + C1);
}
What I think you've done is attach the DropZone script to each box. This means that each box has it's own instance of DropZone. Therefore, when the pointer event fires, it prints that particular instance of C1 and C2.
One way to approach this problem is to declare C1, C2, C3, C4 and C5 as static. This ensures that they are shared throughout all instances of DropZone.
i.e.
static string C1;
static string C2;
static string C3;
static string C4;
static string C5;
Declaring static members require some recoding, but let me know if this works for you.
Answer by EdwinChua · May 08, 2017 at 02:20 PM
@annarmengou94 The problem is in the way you are using:
if (gameObject.name == ("Container (2)"))
{
C2 = eventData.pointerDrag.name;
Debug.Log("C2="+C2);
Debug.Log("C1=" + C1);
}
What I think you've done is attach the DropZone script to each box. This means that each box has it's own instance of DropZone. Therefore, when the pointer event fires, it prints that particular instance of C1 and C2.
One way to approach this problem is to declare C1, C2, C3, C4 and C5 as static. This ensures that they are shared throughout all instances of DropZone.
i.e.
static string C1;
static string C2;
static string C3;
static string C4;
static string C5;
Declaring static members require some recoding, but let me know if this works for you.
Edit: I've done a sample of the DropZone class. This should work.
public class DropZone : MonoBehaviour
{
static string C1;
static string C2;
static string C3;
static string C4;
static string C5;
static string A;
Text resultattxt;
public void OnPointerEnter(PointerEventData eventData)
{
if (eventData.pointerDrag == null)
return;
Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
if (d != null)
{
d.placeHolderParent = this.transform;
}
}
public void OnPointerExit(PointerEventData eventData)
{
if (eventData.pointerDrag == null)
return;
Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
if (d != null && d.placeHolderParent == this.transform)
{
d.placeHolderParent = d.parentToReturnTo;
}
}
public void OnDrop(PointerEventData eventData)
{
Debug.Log(eventData.pointerDrag.name + " dropped on " + gameObject.name);
Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
if (d != null)
{
d.parentToReturnTo = this.transform;
}
if (gameObject.name == ("Container (1)"))
{
C1 = eventData.pointerDrag.name;
Debug.Log("C1=" + C1);
}
if (gameObject.name == ("Container (2)"))
{
C2 = eventData.pointerDrag.name;
Debug.Log("C2=" + C2);
Debug.Log("C1=" + C1);
}
if (gameObject.name == ("Container (3)"))
{
C3 = eventData.pointerDrag.name;
Debug.Log("C3=" + C3);
}
if (gameObject.name == ("Container (4)"))
{
C4 = eventData.pointerDrag.name;
Debug.Log("C4=" + C4);
}
if (gameObject.name == ("Container (5)"))
{
C5 = eventData.pointerDrag.name;
Debug.Log("C5=" + C5);
}
A = C1 + C2 + C3 + C4 + C5;
Debug.Log(A);
Start();
}
public void Start()
{
resultattxt.text = A;
}
}
Answer by antx · May 08, 2017 at 01:19 PM
I'm not really sure how your scene is set up but I would guess, that your DropZone.cs script is attached to each of the UI containers at the top (as seen in your screenshots). This makes them all seperate instances with each their own variables (like C1, C2, C3, C4, C5). When you drop something on the first, it sets its C1 variable, but does not know what the others have set in their variables, and so on.
What you need is a global script that you attach to a single gameobject (make a new empty GO and give it a good name for that)
public class MyDataClass : MonoBehaviour
{
public string c1;
public string c2;
public string c3;
public string c4;
public string c5;
}
Your DropZone script would then change to this:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DropZone : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
{
public Text resultattxt;
public string A;
public MyDataClass myDataClass; // set me up in the inspector
public void OnPointerEnter(PointerEventData eventData)
{
if (eventData.pointerDrag == null)
return;
Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
if (d != null)
{
d.placeHolderParent = this.transform;
}
}
public void OnPointerExit(PointerEventData eventData)
{
if (eventData.pointerDrag == null)
return;
Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
if (d != null && d.placeHolderParent == this.transform)
{
d.placeHolderParent = d.parentToReturnTo;
}
}
public void OnDrop(PointerEventData eventData)
{
Debug.Log(eventData.pointerDrag.name + " dropped on " + gameObject.name);
Draggable d = eventData.pointerDrag.GetComponent<Draggable>();
if (d != null)
{
d.parentToReturnTo = this.transform;
}
if (gameObject.name == "Container (1)")
{
myDataClass.c1 = eventData.pointerDrag.name;
Debug.Log("C1 = " + myDataClass.c1);
}
if (gameObject.name == "Container (2)")
{
myDataClass.c2 = eventData.pointerDrag.name;
Debug.Log("C2 = " + myDataClass.c2);
}
if (gameObject.name == "Container (3)")
{
myDataClass.c3 = eventData.pointerDrag.name;
Debug.Log("C3 = " + myDataClass.c3);
}
if (gameObject.name == "Container (4)")
{
myDataClass.c4 = eventData.pointerDrag.name;
Debug.Log("C4 = " + myDataClass.c4);
}
if (gameObject.name == "Container (5)")
{
myDataClass.c5 = eventData.pointerDrag.name;
Debug.Log("C5 = " + myDataClass.c5);
}
A = myDataClass.c1 + myDataClass.c2 + myDataClass.c3 + myDataClass.c4 + myDataClass.c5;
Debug.Log(A);
Start();
}
public void Start()
{
resultattxt.text = A;
}
}
You see that this one has a new public variable myDataClass. For all of your DropZones set this variable up in the inspector by dragging the new GameObject mentioned above on to it. Your DropZones store now the names no longer in themselfs but in this new class and they also read them from there.
Answer by Bunny83 · May 05, 2017 at 02:02 AM
Uhm, as far as i can tell you have attached your "DropZone" script to each of those 5 container objects, right? However that means that each of those script instances has it's own set of C1 to C5 variables.
When you drag an object onto the first container the instance on that container will react and set it's C1 variable to the name of the dragged object. However if you drag the second object onto the second container the script on the second container will react and you put the name in it's C2 variable. Though the C2 variable of the first container is still not set and the C1 of the second container is also not set. Also all "C" variables in the remainer 3 containers are all not set to anything.
You either want to use a single "manager" script which all those DropZone scripts talk to, or declare the 5 variables as static variable. Static variables don't belong to a specific instance of that script, but to the class itself. So those variables will only exist once in the entire application, even when there is no instance.