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);
}
}