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.