0
最近我一直有一些問題,我的Unity項目。我一直試圖爲每個「x」單位生成檢查點,但由於某種原因,前兩個檢查點成功產卵,但之後沒有任何反應。Unity 2D:檢查點上的檢查點生成無法正常工作
這是我在CheckpointGeneration.cs代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckpointGeneration : MonoBehaviour {
public GameObject checkpointPrefab;
int gap = 5;
List<GameObject> previousCheckpointList;
private void Start()
{
Vector3 targetPos = new Vector3(0, -2.35f);
Instantiate(checkpointPrefab, targetPos,
checkpointPrefab.transform.rotation);
}
private void Update()
{
if (CheckpointController.checkpointsReached != previousCheckpointList && CheckpointController.lastCheckpointReached != null)
{
previousCheckpointList = CheckpointController.checkpointsReached;
Vector3 targetPos = new Vector3(CheckpointController.lastCheckpointReached.transform.position.x + gap, CheckpointController.lastCheckpointReached.transform.position.y);
Instantiate(checkpointPrefab, targetPos, checkpointPrefab.transform.rotation);
}
}
}
這是CheckpointController.cs代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckpointController : MonoBehaviour {
public static List<GameObject> checkpointsReached = new List<GameObject>();
public static GameObject lastCheckpointReached;
GameObject player;
private void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
}
private void Update()
{
if (checkpointsReached.Count > 0)
{
lastCheckpointReached = checkpointsReached[checkpointsReached.Count - 1];
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
checkpointsReached.Add(gameObject);
GetComponent<SpriteRenderer>().color = new Color(0, 0.86f, 0.29f);
}
Debug.Log("Checkpoint Reached!");
}
public void RespawnAtCheckpoint()
{
if (lastCheckpointReached != null)
{
player.transform.parent.position = lastCheckpointReached.transform.position;
player.GetComponent<PlayerManager>().isDead = false;
Camera.main.transform.position = GameObject.FindGameObjectWithTag("CameraFollow").transform.position;
Debug.Log("Respawning...");
} else
{
GameObject.FindGameObjectWithTag("GameController").GetComponent<ReloadLevel>().Reload();
}
}
}
任何答案,將不勝感激!如果您需要更多信息,請發表評論,我將編輯問題以提供。
「CheckpointGeneration」屬於場景中的空遊戲對象,「CheckpointController」屬於檢查點預製? –
@AleksaRistic是的,「CheckpointGeneration」腳本位於標記爲「GameController」的空遊戲對象上,「CheckpointController」位於檢查點預製件上。 –
它是否將它們產生爲gameobjects –