get value in slots

Hello good day i need help for my small project because im new to unity, my problem is how do i combine the answer of slot1 and slot2 and check if its correct?

like for example question #1 the correct answer is 13 and i put a 1 in slot1 and put 3 in slot2 how do i check if this is correct and how do i get the the value of slot?

heres my code and image

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) {
                DragHandle.itemBeingDragged.transform.SetParent(transform);
                ExecuteEvents.ExecuteHierarchy<IHasChanged>(gameObject,null,(x,y) => x.HasChanged ());
        }else
        {
            Transform aux = DragHandle.itemBeingDragged.transform.parent;
            DragHandle.itemBeingDragged.transform.SetParent(transform);
            item.transform.SetParent(aux);
            ExecuteEvents.ExecuteHierarchy<IHasChanged>(gameObject,null,(x,y) => x.HasChanged ());
           
        }
    }
    #endregion

}

2967345--220343--212.PNG

Multiply the left slot by 10 and add the value of the right slot. This also works with more slots. When you have 3 slots multiply the left one by 100, the middle one by 10 and add them all together.
Check this whenever a value has been changed. I can’t tell you how to get the values of the slots, because that depends on your implementations of them.
If you need help there, you will have to explain your implementation a bit better.

So is this script attached to the 2 empty boxes in the image. It looks like when I drop a number on an empty box it makes that box its parent. And if there was already another number there, that number gets parented back to the original parent of what im dragging… which I assume is whatever GO is holding them all the bottom… Thus putting it back?

Naively we can just make a script like this and attach it to each of your boxes:

public class BoxID : MonoBehavior
{
        // Set this in the inspector.  Box 1 = "1"
        public string valString;

        public string GetValue()
        {
                return valString;
        }
}

Then your add this to slot, and your answer checking code can call it:

public string GetAnswer()
{
     if (transform.ChildCount > 0)
     {
             GameObject child = transform.GetChild(0).gameObjet;
             BoxID idScript = child.GetComponent<BoxID>();
             return idScript.GetValue();
     }
     else
          return "";
}

This will work well for any box that you make that has a string for an answer. However you can use interfaces to make it more robust. Lets Say we wanted boxes to return answers based on what color they were (Maybe you add in code that the box changes color by clicking on it.)

First you just make an interface script that isn’t attached to anything:

public interface IBox
{
       void string GetValue();
}

Now our BoxID script stays the same except for one small change:

public class BoxID : MonoBehavior, IBox

our Slot script would change to this:

public string GetAnswer()
{
     if (transform.ChildCount > 0)
     {
             GameObject child = transform.GetChild(0).gameObjet;
             IBox idScript = child.GetComponent<IBox>();
              if (idScript == null)
                      //throw an error since we somehow have box that isn't implementing IBox
             return idScript.GetValue();
     }
     else
         return "";
}

But now our Slot script doesn’t care or know about what kind of class its getting an answer from. We can make any class that implements IBox and the Slot will ask it what value its answer is. So back to our ColorChanging Box:

public class ColorChangeBox : MonoBehavior, IBox
{
         // some list of colors to change between
         List<Color> colors = new List<Color>():
         // and index into the colors;
         int currentColor;

         //Code in here to change the color of the box
         // and update currentColor

        public string GetValue()
        {
                switch(currentColor)
                {
                        case 0:  return "green";
                        case 1:  return "blue";
                        // you get the idea
                 }
          }
}
        }
}

So we can just make this box and stick it right in our scene, and the Slot script will go right on working. It doesn’t care what class its child is as long as its an IBox. Additionally it doesn’t know or care how this class implements GetValue… as long as it returns some string that we can use as an answer. Just to be clear you could have BoxID boxes and ColorChangeboxes in the same scene and it will work seamlessly.

Now the second part of your question is pretty easy. Since each slot is getting a string value from its children in the method GetAnswer() You just have your GameManger call GetAnswer on both slots, and make sure each string is equal to what it should be. Your Question Class should have something like this in it :

string[] CorrectAnswer = new string[2] { "1", "3"};
// or like this and you fill it in the inspector.
// You'll have to set the array size to 2 in the inspector as well
public string[] CorrectAnswer;

And you just make sure slot 0 and slot 1 strings are equal to those.

1 Like

Well, assuming you are checking that both slots have something in them, you’ll have to get a value from those boxes. Either you have a script with a variable on it that stores the value, you set the gameobject name to be the number, or if those are text components on an image, then you can get the text value.

Thank you for this amazing answer, one last question im a bit confuse in this one how do you check it

im sorry i didnt get it because im still learning to code hehehehe.

I’m assuming that the empty Boxes in your image have the Slot script you posted attached to them. The rest of the numbered boxes would have some kind of script attached to them like the BoxID script. So if you modify your slot script to poll the value of its child like I posted then you would have some other GameObject Controller like this:

public class GameController: MonoBehaviour
{
    // Set these in the inspector
    public GameObject slot0;
    public GameObject slot1;

    private QuestionClass  currentQuestion;

    private void SetNextQuestion()
    {
            // Some code here goes to pick the next
            // question and set it to currentQuesiton
     }

    public void CheckAnswer()
    {
           string answer0 = slot0.GetAnswer();
           string answer1 = slot1.GetAnswer();
           if (answer0 == currentQuestion.CorrectAnswer[0] &&
               answer1 == currentQuestion.CorrectAnswer[1])
            {
                    // Do whattever you do when they get it right
            }
            else
            {
                 // Do whatever you do when its wrong
             }
    }
}

I’m assuming you have some QuestionClass setup that holds data for your questions, and you have some code to pick a question. Its hard to give exact code without seeing all the scripts in the entire project and knowing how things are setup. But this is a good general idea of what you should be doing.

The public CheckAnswer on GameController would probably be a ButtonCallback. Since the user is dragging around answers into your slots you need some way for the user to press and OK or Done button to signal they want to check their answer.