Im currently build a game where player will move based on the sequence that added in the list. But, it depends on 2 sequence, Sequence and inside sequence. So I use 2 Coroutine to differentiate both but it does not execute the other Coroutine and stop the game. Can you guys tell me why? Im still beginner in developing the game so I dont know much about the syntax (I did read the manual in unity btw). Thank you!
public void OnPlayButtonClicked()
{
Play();
}
public void Play()
{
sequence.Clear();
sequence=TranslateCodeFromBlocks(canvas_.transform, sequence);
if (sequence.Any(item => item.ID == "Function"))
{
// Code to execute if an item with ID="Function" is found
Debug.Log("Found an item with ID='Function' in the sequence");
loop2 = new MainLoop(mainTarget, insideFunction, animator);
StartCoroutine(loop2.Play());
}
else
{
// Code to execute if no item with ID="Function" is found
Debug.Log("No item with ID='Function' found in the sequence");
loop1 = new MainLoop(mainTarget, sequence, animator);
StartCoroutine(loop1.Play());
}
}
void Start()
{
canvas_ = GameObject.Find("Rectangle");
secondCanvas = GameObject.Find("FunctionBox");
mainTarget = GameObject.Find("Bee").GetComponent<Rigidbody2D>();
animator = GameObject.Find("Bee").GetComponent<AnimationManager>();
}
public List<Function_> TranslateCodeFromBlocks(Transform parent, List<Function_> sequence_)
{
foreach (Transform child in parent)
{
var functionName = child.name.Split('_');
//Debug.Log(functionName);
if (functionName[0] == "Function")
{
string function = functionName[1];
switch (function)
{
case "MoveRight":
sequence_.Add(new MoveRight("MoveRight"));
break;
case "MoveLeft":
sequence_.Add(new MoveLeft("MoveLeft"));
break;
case "MoveUp":
sequence_.Add(new MoveUp("MoveUp"));
break;
case "MoveDown":
sequence_.Add(new MoveDown("MoveDown"));
break;
case "Function":
insideFunction = new List<Function_>();
wrapper = new FunctionWrapper("Function");
sequence_.Add(wrapper);
insideFunction = wrapper.FunctionSequence(secondCanvas.transform, insideFunction);
break;
}
}
}
Debug.Log("Returning sequence: " + sequence_.Count + " items");
return sequence_;
}