hello there! ive got a problem with a hollow knight-like bench system.
upon dying, i want the player to respawn in another scene at a certain point.
which is an object with a set position thisPosition to which this script is attached:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Bench_Home : MonoBehaviour {
private CharacterController2D controller;
public GameManagerScript gm;
static string Load_Home = "Home";
private GameObject player;
public Vector2 thisPosition;
void Start() {
//WICHTIG!!! reference so machen, um auch bei scene change die reference zum script zu behalten
if (gm == null) {
gm = GameObject.FindGameObjectWithTag("Player").gameObject.GetComponent<GameManagerScript>();
}
if (controller == null) {
controller = GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterController2D>();
}
if (player == null) {
player = GameObject.FindGameObjectWithTag("Player");
}
}
void OnTriggerEnter2D(Collider2D other) {
if (other.CompareTag("Player")) {
gm.lastBenchPos = thisPosition;
}
}
void Update() {
thisPosition = new Vector2(-64.01f, -16.01f);
//check for last bench position
if (controller.respawned && thisPosition == gm.lastBenchPos) {
LoadScene(Load_Home);
Physics2D.IgnoreLayerCollision(7, 8, false);
Physics2D.IgnoreLayerCollision(7, 9, false);
player.transform.position = thisPosition;
//check for child object and enable
controller.rb.gravityScale = 3;
}
controller.respawned = false;
}
public static void LoadScene(string SceneNameToLoad) {
PendingPreviousScene = SceneManager.GetActiveScene().name;
SceneManager.sceneLoaded += ActivatorAndUnloader;
SceneManager.LoadScene(SceneNameToLoad, LoadSceneMode.Additive);
Debug.Log("Home was loaded");
}
static string PendingPreviousScene;
static void ActivatorAndUnloader(Scene scene, LoadSceneMode mode) {
SceneManager.sceneLoaded -= ActivatorAndUnloader;
SceneManager.SetActiveScene(scene);
SceneManager.UnloadSceneAsync(PendingPreviousScene);
}
}
when i instantiate my player, i am spawned at the location of gm.lastBenchPos
and upon interaction with another “bench”, i want gm.lastBenchPos to update.
in the inspector nothing happens, but if i put a Debug.Log(lastBenchPos) into Update() of GameManagerScript, the values are changing.
GameManagerScript is attached to the Player(Clone) created from prefab.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManagerScript : MonoBehaviour
{
public Vector2 lastCheckPointPos;
public Vector2 lastBenchPos;
}
i need the values to be able to fullfil the if-statement of the other bench, which has different coordinates.
and make it able for the player to teleport there upon death.
im trying to make this work since 3 full days and im slowly descending into madness because i need to have this figured out. please help me.
thanks in advance!