Share bool var to another class

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.
image

1 Like

Based on the code this is not possible. At least not from this code. The isAlive is public, so it can be changed from anywhere and doesn’t have to go through the IsAlive() method. Make it private. If anything is changing it from outside, it will now break because it will no longer have access. Fix those to use the method instead

Your console log is also not sorted properly. The False appears at the end, but it happened before the True. You can see this by the timestamps in front of it. You probably have these collapsed so you can’t see a proper ‘timeline’ of the events

i’m sorry my description is not clear. the second class is followed:

 [SerializeField] private float rotationSpeed;
    [SerializeField] private float thrustSpeed;
    [SerializeField] private AudioClip mainEngineClip;
    

   
    private Rigidbody rb;
    private AudioSource audioSource;
    private CollisionHandler collisionHandler;
    private void Start()
    {
        rb = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
        collisionHandler = GetComponent<CollisionHandler>();
       
    }
    private void Update()
    {
        
        if(!collisionHandler.GetIsAlive())
        {
            Debug.Log("death");
        }
        if(collisionHandler.GetIsSuccess())
        {
            Debug.Log("success");
        }
        ProcessThrust();
        ProcessRotation();
        //if (!collisionHandler.GetIsAlive() && !audioSource.isPlaying)
        //{
        //    audioSource.PlayOneShot(deathClip);
        //}
        //if (collisionHandler.GetIsSuccess() && !audioSource.isPlaying )
        //{
        //    audioSource.PlayOneShot(sucessClip);
        //}
    }

    private void ProcessRotation()
    {
        if (Input.GetKey(KeyCode.A))
        {

            //Debug.Log("rotate to left");

            ApplyRotation(rotationSpeed);

        }
        if (Input.GetKey(KeyCode.D))
        {

            // Debug.Log("rotate to right");
            ApplyRotation(-rotationSpeed);

        }
    }

    private void ApplyRotation(float rotateThisFrame)
    {
        rb.freezeRotation = true;//can control manully;
        transform.Rotate(Vector3.forward * rotateThisFrame * Time.deltaTime);
        rb.freezeRotation = false;//take control by physics system.
    }

    private void ProcessThrust()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            // Debug.Log("Thrusting");
            rb.AddRelativeForce(Vector3.up * thrustSpeed * Time.deltaTime);
            if (!audioSource.isPlaying)
            {
                audioSource.PlayOneShot(mainEngineClip,0.5f);
            }
        }
        else
        {
            if (audioSource.isPlaying)
            {
                audioSource.Stop();
            }

        }

    }

it is tried to output “death” and “success”, but it is never get it.

Yes, it won’t.

As soon as you crash, you set isAlive to false, but then you also disable the movement. Now, movement is no longer processing Update so the code that checks if you are dead doesn’t run anymore. This is the same for isSuccess.

Thank you very much!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms