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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Help, It's staying false when it shouldn't be.

Discussion in 'Scripting' started by False_Baconator, Jan 5, 2019.

  1. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    so i'm trying to make a 2D dungeon crawler. I'm trying to have it so that the ally will teleport by the player when the ally is off screen. unfortunately, it teleports by the player even when it shouldn't.

    I'm fairly certain I've narrowed the issue down to this area of code here, not turning AllyOnCamChecker to true, but I don't know why.

    Code (CSharp):
    1.             Collider2D[] AllyCamChecker = Physics2D.OverlapBoxAll(CamCenter.position, size, AllyLayer);
    2.  
    3.  
    4.             for (int i = 0; i < AllyCamChecker.Length; i++)
    5.             {
    6.                 if(AllyCamChecker[i] == Ally)
    7.                 {
    8.                     AllyOnCamChecker = true;
    9.                 }
    10.  
    11.             }

    the rest of the code in that area is right here.

    Code (CSharp):
    1.             Collider2D[] AllyCamChecker = Physics2D.OverlapBoxAll(CamCenter.position, size, AllyLayer);
    2.  
    3.  
    4.             for (int i = 0; i < AllyCamChecker.Length; i++)
    5.             {
    6.                 if(AllyCamChecker[i] == Ally)
    7.                 {
    8.                     AllyOnCamChecker = true;
    9.                 }
    10.  
    11.             }
    12.  
    13.             AllyOnCam = AllyOnCamChecker;
    14.  
    15.             if (AllyOnCam == false)
    16.             {
    17.                 FindSpawnPoint();
    18.                 Ally.transform.position = MainSpawn.transform.position;
    19.             }
    20.  
    21.             AllyOnCamChecker = false;
    all this code is in the update area for some code attached to the camera.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Well you assign the AllyOnCamChecker value to AllyOnCam, but only set AllyOnCamChecker to false every frame, so AllyOnCam's block is always going to fire after it receives a value?
     
  3. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    it stays false even if the code setting AllyOnCamChecker to false wasn't there or if it was before everything.

    the reason I'm setting it back to false after everything is so that it gets reset for the next frame.

    I've set up some Debug.Log statements and it seems that the problem was where i suspected.

    Code (CSharp):
    1.             Collider2D[] AllyCamChecker = Physics2D.OverlapBoxAll(CamCenter.position, size, AllyLayer);
    2.  
    3.             for (int i = 0; i < AllyCamChecker.Length; i++)
    4.             {
    5.                 if(AllyCamChecker[i] == Ally) //this if statement isn't being triggered.
    6.                 {
    7.                     AllyOnCamChecker = true;
    8.                     Debug.Log("AllyOnCamChecker should be true.");
    9.                 }
    10.  
    11.                 Debug.Log("AllyCamChecker = " + AllyCamChecker[i]);
    12.  
    13.             }
    also, it seems that AllyCamChecker is checking things that arent in the designated layer.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Well if OverlapBoxAll() isn't getting the correct colliders, you definitely need to start debugging there.
     
  5. False_Baconator

    False_Baconator

    Joined:
    Oct 2, 2018
    Posts:
    35
    I checked the list and it is getting the ally collider, its just also getting all the walls and such.

    After tying that sentence i realized that the if statement was trying to see if a collider was equal to a gameobject, so it never would've worked. I wrote another method in a script attached to the ally, and then used getcomponent().

    It works like a charm now. a slightly rugged charm, but a charm nonetheless. Thanks for the Help!
     
    GroZZleR likes this.