Unsolved Child transform position problem
I have a parent object that has to stop depending on the position of its children. I need the first and only children to stop at say, -10y. However, with the example I will provide, this is not happening. Instead it seems all children have to be below this point.
The two primary scripts use are as follows:
public class Tetro:MonoBehaviour
{
Instantiator instance;
void Start ()
{
instance = FindObjectOfType<Instantiator>();
StartCoroutine(Fall());
}
IEnumerator Fall()
{
foreach(Transform mino in transform)
{
while(mino.position.y > -10)
{
yield return new WaitForSeconds(1);
transform.position -= new Vector3(0, 1);
}
}
StopCoroutine(Fall());
}
public void CheckInput()
{
if (transform.position.y > -10)
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
transform.position += new Vector3(1, 0);
if (!checkEachChildPosition())
{
transform.position += new Vector3(-1, 0);
}
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.position += new Vector3(-1, 0);
if (!checkEachChildPosition())
{
transform.position += new Vector3(1, 0);
}
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
transform.Rotate(0, 0, 90);
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
transform.position -= new Vector3(0, 1);
if (!checkEachChildPosition())
{
transform.position += new Vector3(0, 1);
}
}
}
}
bool checkEachChildPosition()
{
foreach (Transform mino in transform)
{
Vector2 pos = FindObjectOfType<GameManager.GameManager>().RoundOffPositions(mino.position);
if (FindObjectOfType<GameManager.GameManager>().CheckIsInGrid(pos) == false)
{
return false;
}
}
return true;
}
void AllowNext()
{
if (transform.position.y == -10)
{
instance.Spawn();
enabled = false;
}
}
void Update ()
{
CheckInput();
AllowNext();
}
}
And:
namespace GameManager
{
public class GameManager:MonoBehaviour
{
Instantiator instance;
void Start()
{
instance = FindObjectOfType<Instantiator>();
instance.Spawn();
}
public bool CheckIsInGrid(Vector2 pos)
{
return (pos.x >= -7 && pos.x <= 7 && pos.y > -10 && pos.y < 12);
}
public Vector2 RoundOffPositions(Vector2 pos)
{
return new Vector2(Mathf.Round(pos.x), Mathf.Round(pos.y));
}
void Update()
{
}
}
}
I assume the issue is occurring in Fall()
but it shouldn't. Anyone know why this is happening? Or how this can be fixed?
@Sora$$anonymous$$ahiro, there must be some kind of problem with your web connection or something, you keep posting questions twice. Anyway, I'll have a look at it.
Answer by UnityCoach · Dec 13, 2016 at 09:03 PM
Disabling a component doesn't stop its Coroutines. It will simply stop receiving Update calls. You must stop the Coroutine when you disable the component I believe.
I stop the coroutine at the end of the coroutine, so I'm not sure if that is the issue.
I don't understand this : foreach(Transform $$anonymous$$o in transform)
Do you have more than one transform ?
I have a parent object that holds all the children. This iterates over all the children allowing me to check each one in a single loop.
Answer by SoraMahiro · Dec 16, 2016 at 05:29 PM
This isn't a full answer, but I found that the problem originates in CheckIsInGrid()
the problem that seems to be happening, is that when it is called in checkEachChildPosition()
it's not being iterated over ALL transforms in the parent. Why, I don't know yet. I've tried it in a few different projects and received different results.
So, in one project the function seemed to iterate over one axis and not the other, then in another one it seemed to iterate over all axis' but it wasn't stopping the Fall()
routine. I am complete stumped...
Your answer
Follow this Question
Related Questions
Issue with accessing Text by code/how do i acces a Text being spawned. 0 Answers
How do you add a child through code 2 Answers
Can’t keep the position 0 Answers
Moving grabbed object OnTriggerStay2D 0 Answers