【问题标题】:unity change transform object through scriptunity 通过脚本改变变换对象
【发布时间】:2018-10-28 21:24:23
【问题描述】:

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

 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";
        }

    }
}

【问题讨论】:

  • 你检查Pickup是否被破坏,但Pickup是什么?我没有看到它在任何地方定义,如果对象被破坏,你应该检查检查点是否为空,对吗?
  • 嗨!是否在 Pickup 类中检查布尔 isDestroyed。 isDestroyed 布尔值是一个公共静态变量,因此我可以从此类访问它
  • 如果你破坏了 this.gameObject,你也破坏了 this.gameObject 并且被破坏的东西,嗯,被破坏了,不能再做东西了。您可能希望通过实际存在的不同游戏对象来控制破坏以讲述故事;)

标签: c# unity3d transform


【解决方案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";

   }
}

【讨论】:

  • 你好,我试过了,但没有用。我相信我已经找到了问题所在。当我在玩家击中拾取器后销毁拾取器时,它也会破坏对变量的任何引用,因此我无法访问它。我已经通过在层次结构视图的游戏过程中检查 isDestroyed 复选框来测试这一点,并且这样做它可以工作,但是一旦我击中拾取对象并将它破坏它然后它就不起作用了
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2019-12-08
    • 1970-01-01
    • 2019-09-03
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多