繁体   English   中英

通过脚本统一更改转换对象

[英]unity change transform object through script

我有一个脚本,该脚本可通过对象的变换检测对象的距离并将其显示给播放器,我希望目标对象一旦被破坏即可更改,以便目标标记指向另一个对象。 到目前为止,这是我的代码

 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);时拾取脚本被破坏Destroy(this.gameObject); 叫做。

为了解决这个问题,我只破坏了满足条件后不需要使用的组件部分。 我通过编写Destroy(this.gameObject.getComponent<whatever component you want to destroy on the object>()); 我希望这可以帮助其他人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM