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 –