I seen some similar questions posted about this, but none of the ones I read were answered. I wrote a script that simulates a wind zone.
There is a collider on the wind zone game object that turns a bool “inwindzone” true when the player is colliding with it, false if not. When I have a pre-existing game object with the tag “Player” already in the hierarchy and pass the wind zone script the game object from the hierarchy , on game start the script works fine on the player. (Moves the player up for a period of time, pauses movement all together, moves the player down, then pauses) this repeats as long as the player is in the wind zone.
However when I delete the player from the hierarchy, add the player prefab to the wind zone script where it accepts the gameobject player, and have my spawn manager instantiate the player prefab in the scene on start, inside the wind zone, there is no movement. The wind zone does not effect the player prefab.
I thought collision detection might be an issue, but even if I move the instantiated player outside of the wind zone and move it back in the inwindzone bool is true, however nothing else is performed on it, or so it seems. The tag is still labelled player. I’m not sure why this isn’t working, I’m still relatively new to unity and still learning!
My player script just contains the player movement so I didn’t bother to post that, but I have attached my wind zone script and spawn manager script that instantiates the player below:
public class WindArea : MonoBehaviour {
public GameObject player;
public float strength;
public bool inWindZone = false;
public bool movingUp = true;
public bool movingDown = false;
public bool pauseUp = false;
public bool pauseDown = false;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
inWindZone = true;
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
inWindZone = false;
}
}
void Update()
{
{
if (pauseUp)
{
StartCoroutine(DelayUp());
}
else if (pauseDown)
{
StartCoroutine(DelayDown());
}
else if (movingUp)
{
StartCoroutine(Moveup());
}
else if(movingDown)
{
StartCoroutine(Movedown());
}
}
}
IEnumerator Moveup()
{
if(inWindZone)
{
player.transform.Translate(new Vector2(0, 1) * strength * Time.deltaTime);
}
yield return new WaitForSeconds(3);
movingUp = false;
movingDown = false;
pauseDown = false;
pauseUp = true;
}
IEnumerator DelayUp()
{
yield return new WaitForSeconds(5);
movingUp = false;
movingDown = true;
pauseUp = false;
pauseDown = false;
}
IEnumerator Movedown()
{
if (inWindZone)
{
player.transform.Translate(new Vector2(0, -1) * strength * Time.deltaTime);
}
yield return new WaitForSeconds(3);
movingDown = false;
movingUp = false;
pauseUp = false;
pauseDown = true;
}
IEnumerator DelayDown()
{
yield return new WaitForSeconds(5);
movingDown = false;
movingUp = true;
pauseUp = false;
pauseDown = false;
}
}
public class SpawnManager : MonoBehaviour {
[SerializeField]
private GameObject _player;
// Use this for initialization
void Start () {
Instantiate(_player, new Vector2(0, -27), Quaternion.identity);
}
// Update is called once per frame
void Update () {
if (_player.transform.position.y <= -35)
{
StartCoroutine(Respawn());
}
}
IEnumerator Respawn()
{
Destroy(this.gameObject);
yield return new WaitForSeconds(3);
Instantiate(_player, new Vector2(0, -30), Quaternion.identity);
}
}