Search Unity

Question Bool not working correctly, thinks that it's false even though it's true

Discussion in 'Scripting' started by CodeMateo, Aug 12, 2020.

  1. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    Hey guys I'm driving myself crazy with this issue so I would love some help.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. using System.Security.Cryptography;
    5. using UnityEngine;
    6.  
    7. public class RoomSpawner : MonoBehaviour
    8. {
    9.     [Tooltip("The seed that Room will be generated with. Set to a value %lt 0 to have the seed generated randomly on build/generation.")]
    10.     public int seed = -1;
    11.  
    12.     public int openingDirection;
    13.     // 1 --> need bottom door
    14.     // 2 --> need top door
    15.     // 3 --> need left door
    16.     // 4 --> need right door
    17.  
    18.     private RoomTemplates templates;
    19.     private AddRoom add;
    20.     private int rand;
    21.     public bool spawned = false;
    22.  
    23.     private float waitTime = 20f;
    24.     private RoomMax nr;
    25.     private SetSeed sSeed;
    26.  
    27.     void Start()
    28.     {
    29.         //sSeed = GameObject.FindGameObjectWithTag("MainRooms").GetComponent<SetSeed>();
    30.         //UnityEngine.Random.InitState(sSeed.seed);
    31.         //causing the EnemySpawner to have the same seed;
    32.  
    33.         //Destroy(gameObject, waitTime);
    34.         templates = GameObject.FindGameObjectWithTag("MainRooms").GetComponent<RoomTemplates>();
    35.         add = gameObject.GetComponentInParent<AddRoom>();
    36.         Invoke("Spawn", 1f);
    37.         nr = GameObject.FindWithTag("MainRooms").GetComponent<RoomMax>();
    38.     }
    39.  
    40.     void Spawn()
    41.     {
    42.  
    43.         if (templates == null)
    44.         {
    45.             UnityEngine.Debug.Log("templates not been set");
    46.         }
    47.         if (spawned == false)
    48.         {
    49.             if (nr.numberRoom <= nr.maxRoom)
    50.             {
    51.                 if (openingDirection == 0)
    52.                 {
    53.                 }
    54.                 if (openingDirection == 1)
    55.                 {
    56.                     rand = Random.Range(0, templates.bottomRooms.Length);
    57.                     Instantiate(templates.bottomRooms[rand], transform.position, templates.bottomRooms[rand].transform.rotation);
    58.                     nr.numberRoom += 1f;
    59.                 }
    60.                 else if (openingDirection == 2)
    61.                 {
    62.                     rand = Random.Range(0, templates.topRooms.Length);
    63.                     Instantiate(templates.topRooms[rand], transform.position, templates.topRooms[rand].transform.rotation);
    64.                     nr.numberRoom += 1f;
    65.                 }
    66.                 else if (openingDirection == 3)
    67.                 {
    68.                     rand = Random.Range(0, templates.leftRooms.Length);
    69.                     Instantiate(templates.leftRooms[rand], transform.position, templates.leftRooms[rand].transform.rotation);
    70.                     nr.numberRoom += 1f;
    71.                 }
    72.                 else if (openingDirection == 4)
    73.                 {
    74.                     rand = Random.Range(0, templates.rightRooms.Length);
    75.                     Instantiate(templates.rightRooms[rand], transform.position, templates.rightRooms[rand].transform.rotation);
    76.                     nr.numberRoom += 1f;
    77.                 }
    78.                 spawned = true;
    79.                 //UnityEngine.Debug.Log(rand + " " + transform.position);
    80.             }
    81.             if (nr.numberRoom > nr.maxRoom)
    82.             {
    83.                 templates.allDone = true;
    84.                 Invoke("SpawnClose", 1f);
    85.             }
    86.         }
    87.     }
    88.  
    89.     void SpawnClose()
    90.     {
    91.         if (spawned == false)
    92.         {
    93.             if (nr.numberRoom > nr.maxRoom)
    94.             {
    95.                 if (openingDirection == 1)
    96.                 {
    97.                     Instantiate(templates.ClosedbottomRooms, transform.position, templates.ClosedbottomRooms.transform.rotation);
    98.                 }
    99.                 else if (openingDirection == 2)
    100.                 {
    101.                     Instantiate(templates.ClosedtopRooms, transform.position, templates.ClosedtopRooms.transform.rotation);
    102.                 }
    103.                 else if (openingDirection == 3)
    104.                 {
    105.                     Instantiate(templates.ClosedleftRooms, transform.position, templates.ClosedleftRooms.transform.rotation);
    106.                 }
    107.                 else if (openingDirection == 4)
    108.                 {
    109.                     Instantiate(templates.ClosedrightRooms, transform.position, templates.ClosedrightRooms.transform.rotation);
    110.                 }
    111.                 spawned = true;
    112.             }
    113.         }
    114.     }
    115.  
    116.     void OnTriggerEnter2D(Collider2D other)
    117.     {
    118.         if (other.CompareTag("SpawnPoint"))
    119.         {
    120.             if (spawned == false && other.GetComponent<RoomSpawner>().spawned == false)
    121.             {
    122.                 Instantiate(templates.closedRooms, transform.position, templates.closedRooms.transform.rotation);
    123.                 Destroy(gameObject);
    124.                 spawned = true;
    125.             }
    126.  
    127.             /*if (spawned == false && other.GetComponent<RoomSpawner>().spawned == true)
    128.             {
    129.                 UnityEngine.Debug.Log("Worked" + " " + transform.position);
    130.                 spawned = true;
    131.             }*/
    132.         }
    133.     }
    134. }
    On line 127, it's supposed to only say "Worked" if spawned = false, but for some reason it thinks that every room tile is spawned = false even after they should equal spawned = true. It's hard to explain the whole problem I can add to it if anyone asks. Thank you
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You only ever set the bool to true in that script so as it is public, where else it is referenced? Is another script setting it to false?
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I'm confused because you are destroying the gameObject the script is on, yet setting spawned = true right after. What are you trying to accomplish with that?

    The commented code when uncommented, are you saying it always runs?
     
  4. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    shoot I meant to delete that and forgot to sorry about that

    yes, the commented code when un commented will run when the game object that it’s on hits another game object that has the same script. It will then compare the two seeing which is spawned = true/false and then determine an action
     
  5. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    There’s several of these scripts, they’re supposed to compare to one and another
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    So, the gameobject this script is on when it collides with a SpawnPoint checks if it's condition is false and the only determining factor for the if checks is if the other script is true or false.

    I'm trying to understand what issue you are running into. Is it one if statement isn't ever running? If that is the case, make sure you are debugging what the other scripts spawned value is.

    Also, what @WarmedxMints was saying is the value of spawned starts as false but ONLY gets set to true in the script and never back to false, which means that once it's true it is always true unless being changed by another script.
     
  7. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    yes true

    that’s the thing, it’s ALWAYS saying it’s “worked” for each object. Which is weird because the object has already spawned something and should be true not false.

    yes I want it to stay true, it determines which rooms have been placed and which haven’t
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Have you added a debug to the OnTrigger call to print out what value spawned is?
     
  9. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    I should do that, I’ll do it when I get back home to my laptop
     
  10. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    do you not need to set the other script object to true as well?
     
  11. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    I set it to true if it spawns a room, I’m trying to create a system that doesn’t have rooms collide