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
1
Question by annarmengou94 · May 04, 2017 at 02:01 PM · variablesif-statementsdrag and drop

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.

alt text alt text

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


 

}

2.jpg (63.4 kB)
3.jpg (74.2 kB)
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 EdwinChua · May 05, 2017 at 01:23 AM 0
Share

@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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
     }
 }
Comment
Add comment · Share
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
0

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.

Comment
Add comment · Share
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
0

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.

Comment
Add comment · Share
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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity thinks one of my variables is a System.type 0 Answers

Why can't won't my if then statements work? 1 Answer

Is it possible to save a transform.position in a single variable? 1 Answer

Text being part of a function? 1 Answer

javascript: declaring a variable in 'while' loop 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