Search Unity

Bug Boolean not changing

Discussion in 'VR' started by unity_C344DB5470DEE7ECD137, Jul 25, 2021.

  1. unity_C344DB5470DEE7ECD137

    unity_C344DB5470DEE7ECD137

    Joined:
    Jul 3, 2021
    Posts:
    8
    Objective - To change the position of door(PortalGuard) if user acquires 4 keys and touches the door.
    Problem - bool key1, key2, key3, key4 are not updating to true.
    I changed then to be true at once and then the door opened. But I want to initialise them at false and chage to true one by one once user touches them.
    Code attached below.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class KeyAcquired : MonoBehaviour
    7. {
    8.     public GameObject key1;
    9.     public GameObject key2;
    10.     public GameObject key3;
    11.     public GameObject key4;
    12.     public GameObject portalGuard;
    13.     public GameObject portalGuardText;
    14.     public ParticleSystem accquiredEffect;
    15.  
    16.     private bool gkey1;
    17.     private bool gkey2;
    18.     private bool gkey3;
    19.     private bool gkey4;
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         Debug.Log("Keys Acquired Script is running");
    25.         gkey1 = false;
    26.         gkey2 = false;
    27.         gkey3 = false;
    28.         gkey4 = false;
    29.     }
    30.  
    31.     // Update is called once per frame
    32.  
    33.     public void OnTriggerEnter(Collider other)
    34.     {
    35.         if (other.gameObject.tag == "Key1")
    36.         {
    37.             gkey1 = true;
    38.             Instantiate(accquiredEffect, key1.transform.position, transform.rotation);
    39.             key1.transform.position = new Vector3(5000, 5000, 5000);
    40.         }
    41.         if (other.gameObject.tag == "Key2")
    42.         {
    43.             gkey2 = true;
    44.             Instantiate(accquiredEffect, key2.transform.position, transform.rotation);
    45.             key2.transform.position = new Vector3(5000, 5000, 5000);
    46.         }
    47.         if (other.gameObject.tag == "Key3")
    48.         {
    49.             gkey3 = true;
    50.             Instantiate(accquiredEffect, key3.transform.position, transform.rotation);
    51.             key3.transform.position = new Vector3(5000, 5000, 5000);
    52.         }
    53.         if (other.gameObject.tag == "Key4")
    54.         {
    55.             gkey4 = true;
    56.             Instantiate(accquiredEffect, key4.transform.position, transform.rotation);
    57.             key4.transform.position = new Vector3(5000, 5000, 5000);
    58.         }
    59.         if(other.gameObject.tag == "PortalGuard")
    60.         {
    61.             if(gkey1 && gkey2 && gkey3 && gkey4)
    62.             {
    63.                 Instantiate(accquiredEffect, portalGuard.transform.position, transform.rotation);
    64.                 portalGuard.transform.position = new Vector3(5000, 5000, 5000);
    65.                 portalGuardText.transform.position = new Vector3(5000, 5000, 5000);
    66.             }
    67.         }
    68.     }
    69. }
    70.  
     
  2. jorgeolothar

    jorgeolothar

    Joined:
    Mar 27, 2016
    Posts:
    26
    Are your colliders set to trigger mode?