I need to add 1/-1 to an int but unity adds 2/-2, any help?,how do I add one to a int"

I need to add 1/-1 to an int whenever my player enters/exits a room through a door, however though, Unity adds 2/-2 instead of 1/-1 any help? (The int is back)

My code looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement; 
public class Restart : MonoBehaviour
{
    public int back = 1;   
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    void OnTriggerEnter2D(Collider2D hit)  
    {
        if (hit.gameObject.tag == "Killfloor")
        {
            int CSnum = SceneManager.GetActiveScene().buildIndex;
            SceneManager.LoadScene(CSnum - back);
            Destroy(gameObject);
            return;
        }
    }
    public void BACK(int B)
    {
        if (B < 0)
        {
            back = back + B;
            back = back + 1; 
            Debug.Log("Negative");
        }
        if (B > 0)
        {
            back = back + B;
            back = back - 1;
            Debug.Log("positive"); 
        }
    }
}

the Finish script calls on the public void, so in case you need it, here it is

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

public class Finish : MonoBehaviour
{
    public GameObject Player;  
    public int NextLevel;
    private bool isopen = false;
    public Animator anim;
    private int B; 
    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        B = NextLevel; 
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    void OnTriggerEnter2D(Collider2D hit)   
    {
        if (hit.gameObject.tag == "Player" && isopen == true)
        {
            DontDestroyOnLoad(Player);
            Restart r = hit.GetComponent<Restart>();
            if (r != null)
            {
                r.BACK(B); 
            }
            int CSnum = SceneManager.GetActiveScene().buildIndex;
            SceneManager.LoadScene(CSnum+NextLevel);
            Destroy(gameObject); 
        }
    }
    public void open()
    {
        isopen = true;
        anim.SetBool("isopeningame", true); 
    }
}

I guess the “int” that you abstractly mentioned in your question is that “back” variable inside your Restart script? Your “BACK” method actually does two things at once to the variable. You always add the value that is passed to the method as well as one additional “1”. So the behaviour of your BACK method is exactly this:

// Each example assumes "back" is initially 0. The comment gives the state of back after the call

BACK(0);     // back == 0
BACK(1);     // back == 2
BACK(3);     // back == 4
BACK(-1);     // back == -2
BACK(-4);     // back == -5

If your method is supposed to add or subtract just “1”, that’s not what you have programmed. However it’s not clear what that method is supposed to do exactly. You essentially pass “NextLevel” to the BACK method. I don’t know why you first copy that value into the “B” variable. However the name “NextLevel” seems to indicate that it holds the index of the next level. Since level indices are always positive i don’t see a reason why this would ever be negative.

Please keep in mind that we only see and know those things you show us or tell us. You also talk about entering and leaving rooms, however we have no idea how you setup your triggers and what values you set there. All we can say, if you pass a non-zero value to your BACK method it will add that value (+/- 1) to your back variable.

See I’m attempting to create a small Metroidvania, meaning you can go to previous levels, that’s why the build index in the script would be a negative number. in my level setup, you would shoot a door, then walk through it to make it to a different scene. What I’m trying to do is for the restart, send the player to the first scene, which has a build index of one. Therefore throughout each scene, I have to add or subtract that levels build index (at the first level there is a door setting the value to 1 that’s why it starts at 0) in order to send the player to the beginning when they die.

Because you have a public “back” variable I can not know what value it has in the scene. But one thing to help you better tracking your issue would be to replace entire your “Back()” method with a one liner like:

back += B + Math.Sign(B);

@tttyd224_unity