Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Changing the state of a bool

Discussion in 'Scripting' started by mikemuk01, Jun 16, 2020.

  1. mikemuk01

    mikemuk01

    Joined:
    Dec 1, 2016
    Posts:
    53
    In my game the player must collect a vaccine before he can move on to the next scene. This C# script is attached to the vaccine game object:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GetVaccine : MonoBehaviour
    7. {
    8.     public GameObject Vaccine;
    9.     public GameObject VaccineText;
    10.     public bool gotVaccine;
    11.  
    12.     void OnTriggerEnter(Collider other)
    13.     {
    14.         StartCoroutine(PickVaccine());
    15.     }
    16.     IEnumerator PickVaccine()
    17.     {
    18.      
    19.         VaccineText.SetActive(true);        // display a text message telling the player he now has the vaccine
    20.         yield return new WaitForSeconds(2); // wait for 2 seconds
    21.         VaccineText.SetActive(false);       // remove the text
    22.         Vaccine.SetActive(false);           // remove the vaccine game object from the scene
    23.     }
    24. }
    I then need to record the bool gotVaccine as true but I can't work out how to do that. The bool needs to be available in another script for when the player tries to leave the scene, as a test for whether or not he has the vaccine. Can anybody help please?
     
    Last edited by a moderator: Jun 16, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Um... Have you tried
    Code (CSharp):
    1. gotVaccine = true;
     
  3. mikemuk01

    mikemuk01

    Joined:
    Dec 1, 2016
    Posts:
    53
    PraetorBlue Thank you. I was over-complicating things when the solution turns out to be simple. Can you help me further by telling me how the bool result can be used in another script?
     
  4. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Have the bool public or have a public method to return the value of the bool
     
  5. mikemuk01

    mikemuk01

    Joined:
    Dec 1, 2016
    Posts:
    53
    Thank you again for your help It works :p