统一通过脚本改变变换对象



我有一个脚本,它通过对象的变换来检测对象的距离,并将其显示给玩家,我希望目标对象在被破坏后发生变化,以便目标标记指向不同的对象。这是我到目前为止的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class DistanceToPickup : MonoBehaviour {
// Reference to checkpoint position
[SerializeField]
private Transform checkpoint;
// Reference to UI text that shows the distance value
[SerializeField]
private Text distanceText;
GameObject exit;
// Calculated distance value
private float distance;

private void Start()
{
exit = GameObject.Find("Door_01");
}

// Update is called once per frame
private void Update()
{
// Calculate distance value between character and checkpoint
distance = (checkpoint.transform.position - transform.position).magnitude;
// Display distance value via UI text
// distance.ToString("F1") shows value with 1 digit after period
// so 12.234 will be shown as 12.2 for example
// distance.ToString("F2") will show 12.23 in this case
distanceText.text = "Pickup: " + distance.ToString("F1") + " meters";
if (Pickup.isDestroyed == true)
{
//point transform checkpoint to new object
checkpoint = exit.transform;
distanceText.text = "Exit: " + distance.ToString("F1") + " meters";
}
}
}

我已经找到了我想指向标记的游戏对象,并将检查点标记更改为指向出口变换,但它没有更改距离文本,我看不出代码有任何问题。任何帮助都将不胜感激

拾取类

public class Pickup : MonoBehaviour {
[SerializeField]
public GameObject m_ExplosionPrefab;
private ParticleSystem m_ExplosionParticles;
private int pickupCounter = 0;
[SerializeField]
private Text PickupText;
public static bool isDestroyed;

private void Update()
{
PickupText.text = "Pickups: " + pickupCounter + " /1";
}
private void OnEnable()
{
// Instantiate the explosion prefab and get a reference to the particle system on it.
m_ExplosionParticles = Instantiate(m_ExplosionPrefab).GetComponent<ParticleSystem>();
// Disable the prefab so it can be activated when it's required.
m_ExplosionParticles.gameObject.SetActive(false);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
Destroy(this.gameObject);
pickupCounter++;
isDestroyed = true;
// Move the instantiated explosion prefab to the tank's position and turn it on.
m_ExplosionParticles.transform.position = transform.position;
m_ExplosionParticles.gameObject.SetActive(true);
// Play the particle system of the tank exploding.
m_ExplosionParticles.Play();
PickupText.text = "Pickups: " + pickupCounter + " /1";
}
}
}

尝试更改此:

private void Update()
{
// Calculate distance value between character and checkpoint
distance = (checkpoint.transform.position - transform.position).magnitude;
// Display distance value via UI text
// distance.ToString("F1") shows value with 1 digit after period
// so 12.234 will be shown as 12.2 for example
// distance.ToString("F2") will show 12.23 in this case
distanceText.text = "Pickup: " + distance.ToString("F1") + " meters";
if (Pickup.isDestroyed == true)
{
//point transform checkpoint to new object
checkpoint = exit.transform;
distanceText.text = "Exit: " + distance.ToString("F1") + " meters";
}
}

到此:

private void Update()
{
if (Pickup.isDestroyed == true)
{
//point transform checkpoint to new object
checkpoint = exit.transform;
distanceText.text = "Exit: " + distance.ToString("F1") + " meters";
}
else
{
// Calculate distance value between character and checkpoint
distance = (checkpoint.transform.position - transform.position).magnitude;
// Display distance value via UI text
// distance.ToString("F1") shows value with 1 digit after period
// so 12.234 will be shown as 12.2 for example
// distance.ToString("F2") will show 12.23 in this case
distanceText.text = "Pickup: " + distance.ToString("F1") + " meters";
}
}

我设法找到了一个解决方法。该代码不起作用,因为我试图访问Pickup脚本中的一个变量,但当调用Destroy(this.gameObject);时,Pickup脚本会被破坏。

为了解决这个问题,我只销毁了组件的一部分,一旦满足条件,我就不需要使用这些部分。我通过写Destroy(this.gameObject.getComponent<whatever component you want to destroy on the object>());做到了这一点,我希望这能帮助其他人。

最新更新