I am getting a warning on 49, Bool Variable For State

I already got as far as 51, How To Trigger Particles, ignoring the error, but it’s getting annoying, and I everything I did with the bool just isn’t working and I have no idea why. This is my code.

using UnityEngine;

using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour

{

[SerializeField] float levelLoadDelay = 2f;

[SerializeField] AudioClip crash;

[SerializeField] AudioClip success;

[SerializeField] ParticleSystem crashParticles;

[SerializeField] ParticleSystem successParticles;

AudioSource audioSource;

bool isTransitioning = false;

void Start () {

    audioSource = GetComponent<AudioSource>();

}

private void OnCollisionEnter(Collision other) {

    if (isTransitioning) { return; }

    switch (other.gameObject.tag) {

        case "Friendly":

            Debug.Log("We hit our safe zone we are now ready to thrust.");

            break;

        case "Finish":

            StartSuccessSequence();

            break;

        default:

            StartCrashSequence();

            Debug.Log("Brace for impact, we are about to crash into--");

            break;

    }

}

void StartSuccessSequence() {

    bool isTransitioning = true;

    audioSource.Stop();

    audioSource.PlayOneShot(success);

    successParticles.Play();

    GetComponent<Movement>().enabled = false;

    Debug.Log("We have now reached our designated location.");

    Invoke ("LoadNextLevel", levelLoadDelay);

}

void StartCrashSequence() {

    bool isTransitioning = true;

    audioSource.Stop();

    audioSource.PlayOneShot(crash);

    crashParticles.Play();

    GetComponent<Movement>().enabled = false;

    Invoke ("ReloadLevel", 1f);

   

}

void LoadNextLevel() {

    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

    int nextSceneIndex = currentSceneIndex + 1;

    if (nextSceneIndex == SceneManager.sceneCountInBuildSettings) {

        nextSceneIndex = 0;

    }

    SceneManager.LoadScene(nextSceneIndex);

    Debug.Log("NEXT");

}

void ReloadLevel() {

    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

    SceneManager.LoadScene(currentSceneIndex);

}

}

What’s the warning?

I can see several mistakes here, though. You keep declaring new isTransitioning variables. Your OnCollisionEnter() will always run the code because you are never changing the value of isTransitioning. In both StartSuccessSequence() and StartCrashSequence() you create a new variable called isTransitioning and setting that value.

1 Like

my bad, just realized the issue, I declared more bools when I meant to reference it. It is working fine now.

Yeah, I just realized that as we speak, it’s working fine now.

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

Privacy & Terms