i have a bool that is never set to true even though it should never be false (what)

i have a bool that SHOULD always be set to true but it is always false and i
i use it in two scripts
only one assigns it at all (as true) and thats this one (other one just checks with a ==)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class sundialCollisions : MonoBehaviour
{
    public GameObject godobj;
    public int checkX = 0;
    public int checkY = 0;
    public bool sundMove = true;

    public void CheckNext(string direction)
    {
            if (direction.Equals("a"))
            {
                checkX -= 100;
                transform.position = transform.position + new Vector3Int(checkX, checkY, 0);
               
            }
            if (Input.GetKey("d"))
            {
                checkX += 100;
                transform.position = transform.position + new Vector3Int(checkX, checkY, 0);
            }
            if (Input.GetKey("w"))
            {
                checkY += 100;
                transform.position = transform.position + new Vector3Int(checkX, checkY, 0);
            }
            if (Input.GetKey("s"))
            {
                checkY -= 100;
                transform.position = transform.position + new Vector3Int(checkX, checkY, 0);
            }

    }

    void OnCollisionStay2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "barrier")
        {
            sundMove = true;
        }
    }
    void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "barrier")
        {
            sundMove = true;
        }
    }

it doesnt register collisions and sundMove is always false for literally no reason and i have been losing my mind for the past four hours
it moves correctly which is weird because even though it moves it doesnt register any collisions at all even though i have a rigidbody on it, a box collider, and the thing i want it to hit had a rigidbody and tilemap collider and i did not have a problem like this earlier when i was doing something similar
heres the thing this script is attached to

and heres the other thing

i feel like i am being gaslit by my own program please send help i will love you forever

You get used to it. My programs do it all the time. :slight_smile:

But take a deep breath and point at it and say “I WILL FIGURE YOU OUT! I WILL DESTROY THIS BUG!”

Make sure there’s not more than one item, you’re on the right script, colliders, etc.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

1 Like

That helped narrow it down a bit- thanks : 0

i’m still very stuck though, its these two methods that are never running (whichdoesntreally explain why initialising the bool to true does nothing but itsastart) and i really really do not understand why?

void OnCollisionStay2D(Collision2D collision)
    {
        Debug.Log("colstay");
        if (collision.gameObject.tag == "barrier")
        {
            sundMove = true;
        }
       
    }
    void OnCollisionExit2D(Collision2D collision)
    {
        Debug.Log("colexit");
        if (collision.gameObject.tag == "barrier")
        {
            sundMove = true;
        }
    }

it just isnt registering collisions even though essentially the same code worked on a different set of gameobjects. would you have any idea how to fix that ? thank you so much for responding so quickly : )

Ooooh, don’t do this. Going straight to the transform effectively bypasses physics.

You have to put a Rigidbody(2D) on this thing if you don’t have one already, then use the .MovePosition() call on the RB2D to move it yourself.

[QUOTE= .MovePosition() call on the RB2D to move it yourself.[/QUOTE]

ahhh i was sort of avoiding using that because i have no idea how it works-
theres a bunch of fields and i dont want to use a fixed update/mess with velocity or whatever input is
i looked for a while if i can just move it just by a set number on an axis but i couldnt find anything.rb.movePosition(transform.position + Input * Time.deltaTime * speed);
im not exactly sure what to do with this constructor to be honest : ([/QUOTE]

C# is case sensitive.

this: rb.movePosition(

is not

this: rb.MovePosition()

Only one is correct, and the reference way to verify is to go straight for the documentation. :slight_smile: