Script not reading outside variables correctly

I’m having an issue with my code. I have a public bool set up in one script, and then in a different one I have an if statement checking if that bool is true. the issue is, whenever the first script makes it true (I used Debug.Log to confirm it was being changed to true) the other script still reads it as false. (Again, used Debug.Log to confirm it thinks it’s false) What makes this even stranger, is that I have these scripts hooked up to game objects, and then I have a prefab of the game object with the first script. Whenever I drag it onto a scene for the first time, it works fine. But if I save it, change scenes, and then come back, it’s then that it stops working. I’m very confused by this.

//can use powerup variables
    public bool isNinja;
    public bool canGlide;
    public bool hammerTime;
    public bool canShoot;
    //cosmetics
    public GameObject hammer;
    public GameObject gun;
    public GameObject ninjaMask;
    public GameObject glider;

    private void OnTriggerEnter2D(Collider2D collisionInfo)
    {
        if (collisionInfo.CompareTag("Player"))
        {
            if(gameObject.name == "Ninja Powerup")
            {
                isNinja = true;
                ninjaMask.gameObject.SetActive(true);
            }
            if(gameObject.name == "Glider Powerup")
            {
                canGlide = true;
            }
            if(gameObject.name == "Hammer Powerup")
            {
                hammerTime = true;
                hammer.gameObject.SetActive(true);
            }
            if(gameObject.name == "Gun Powerup")
            {
                canShoot = true;
                gun.gameObject.SetActive(true);
            }
        }
    }

It happens to every single one of these variables. But I could swear it didn’t at first, and it feels like it slowly spread to each of them. The problem also happens on all other scripts, and not just to if statements, and not just to bools, so I don’t feel the need to share that part of code. Let me know if you still want to see it though.

Never mind! So, I had multiple copies of this script attached to several different Game Objects in an attempt to avoid making several really simple scripts. Apparently that’s a big no no, because they all share variables and at the just because 1 of them makes a variable true, the rest just make it false again. I’m glad I was able to catch this before Nina had to help me again with this really obvious at the end problem. Your the best Nina!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms