I'm making an endless runner game where platforms move towards the player so it makes it seem like the player is running across them. I'm trying to make it so that when the player dies, the platforms stop moving. What I have isn't working, when the player dies, the platforms just keep moving as if nothing has happened. Here is the script I have that is attached to the player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CoinColliderScript : MonoBehaviour
{
void OnTriggerEnter(Collider target)
{
if (target.tag == "Enemy")
{
StartCoroutine(Died());
}
}
IEnumerator Died()
{
PlatformMover.dead = true;
gameObject.GetComponentInParent<Animator>().Play("Male Died");
yield return new WaitForSeconds(0.5f);
SceneManager.LoadScene("MainMenu");
}
}
Here is the script I have that is attached to the platforms:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformMover : MonoBehaviour
{
private Vector3 newPlatPos;
public float platMoveSpeed;
public static bool dead;
public void Start()
{
dead = false;
}
void Update()
{
if (dead)
{
newPlatPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
} else if (!dead)
{
newPlatPos = new Vector3(transform.position.x, transform.position.y, transform.position.z - platMoveSpeed);
}
transform.position = newPlatPos;
}
}
New Code:
Attached to platforms:
public class PlatformMover : MonoBehaviour
{
private Vector3 newPlatPos;
public float platMoveSpeed;
void Update()
{
if (!CoinColliderScript.Dead)
{
newPlatPos = new Vector3(transform.position.x, transform.position.y, transform.position.z - platMoveSpeed * Time.deltaTime);
}
transform.position = newPlatPos;
}
}
Attach to player:
public class CoinColliderScript : MonoBehaviour
{
private static bool dead = false;
public static bool Dead
{
get => dead;
set
{
dead = value;
print("'Dead' changed to " + value);
}
}
void OnTriggerEnter(Collider target)
{
if (target.tag == "Enemy")
{
StartCoroutine(Died());
}
}
IEnumerator Died()
{
dead = true;
gameObject.GetComponentInParent<Animator>().Play("Male Died");
yield return new WaitForSeconds(0.5f);
SceneManager.LoadScene("MainMenu");
}
}
public void Start() { dead = false; }
\$\endgroup\$