Search Unity

Bug Boolean not changing value

Discussion in 'Scripting' 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 gkey1, gkey2, gkey3, gkey4 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. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    So have you debugged it? It's so simple which means either OnTriggerEnter is not being called or the colliders being hit are not tagged exactly as you've put.

    Please note BUG is normally used to indicate a suspected bug in Unity and most of those tags you've added are not relevant to your post.
     
  3. unity_C344DB5470DEE7ECD137

    unity_C344DB5470DEE7ECD137

    Joined:
    Jul 3, 2021
    Posts:
    8
    I started learning c# and unity few weeks ago, so noob here. Sorry for wrong tags sir.

    OnTriggerEnter is being called and colliders are also being hit as Instantiate and transform.position are working. Only the gkey value is not changing to true.

    And if I initialise gkey values to true and remove gkey change codes, last if is working. So I have pin pointed the problem to gkey value not changing. Please help me out sir.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Something else is going wrong then because it's impossible for it to NOT execute the preceeding line i.e. change it to true but then run the instantiate call/position changes. It really does look like you're making some wrong assumption some where.

    Placing "Debug.Log()" where you set them to true would clearly indicate if you are really going there. Also, you can make them public temporarily so you can see them changing in the inspector.