public class CollisionHandler : MonoBehaviour
{
private Movement movement;
private int currentSceneIndex;
public bool isAlive;
public bool isSuccess;
private void Start()
{
movement = GetComponent<Movement>();
currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
isAlive = true;
isSuccess = false;
}
private void Update()
{
Debug.Log(isAlive);
}
private void OnCollisionEnter(Collision collision)
{
switch (collision.gameObject.tag)
{
case "Friendly":
Debug.Log("this thing is friendly!");
break;
case "Finished":
Debug.Log("this thing is finished");
isSuccess = true;
//transform.position = new Vector3(collision.transform.position.x, transform.position.y, collision.transform.position.z);
StartSuccessSequence();
break;
case "Fuel":
Debug.Log("add more fuel!");
Destroy(collision.gameObject);
break;
case "Ground":
Debug.Log("Ground crash!");
isAlive = false;
StartCrashSequence();
//ReloadLevle();
break;
default:
Debug.Log("pull up!");
break;
}
}
private void StartCrashSequence()
{
movement.enabled = false;
Invoke("ReloadLevel", 3f);
}
private void StartSuccessSequence()
{
movement.enabled = false;
Invoke("NextLevel", 3f);
}
private void NextLevel()
{
int nextSceneIndex = currentSceneIndex + 1;
if (nextSceneIndex >= SceneManager.sceneCountInBuildSettings)
{
SceneManager.LoadScene(0);
}
SceneManager.LoadScene(currentSceneIndex + 1);
movement.enabled = true;
}
private void ReloadLevel()
{
SceneManager.LoadScene(currentSceneIndex);
movement.enabled = true;
}
public bool GetIsAlive()
{
Debug.Log("isAlive:" + isAlive);
return isAlive;
}
public bool GetIsSuccess()
{
return isSuccess;
}
}//class
when i tried to share isalive to another class by a method(GetIsAlive), i found it the isAlive is not changed the staus.
case "Ground":
Debug.Log("Ground crash!");
isAlive = false;
StartCrashSequence();
//ReloadLevle();
break;
in above code, the is alive was set a false. but it is received a true in another class.