我有一个脚本,该脚本可以通过对象的变换来检测对象的距离并将其显示给播放器,我希望目标对象一旦被破坏就进行更改,以便目标标记指向另一个对象。到目前为止,这是我的代码
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";
}
}
}
答案 0 :(得分:0)
尝试更改此内容:
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";
}
}
答案 1 :(得分:0)
我设法找到了解决方法。该代码无法正常工作,因为我试图访问Pickup脚本中的变量,但是调用Destroy(this.gameObject);
时,Pickup脚本被破坏了。
要解决这个问题,我只破坏了满足条件后不需要使用的组件部分。我通过写Destroy(this.gameObject.getComponent<whatever component you want to destroy on the object>());
做到了这一点,希望对其他人有帮助。