【问题标题】:Object is not triggering collider对象未触发对撞机
【发布时间】:2019-06-12 23:44:04
【问题描述】:

嗨,我的问题是在 Unity 中,我是 c# 的初学者,我的游戏对象没有触发在游戏平面上设置的对撞机,以便重置它的位置。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BasketballSpawnScript : MonoBehaviour
{ 
    public Transform respawnPoint;

    void OnTriggerEnter(Collider other)
    {    
        if (other.gameObject.CompareTag("Basketball"))
        {    
            other.gameObject.transform.position = respawnPoint.position;
        }    
    }
}

这个脚本附加在平面上,并且游戏对象被标记为篮球,当它进入地板的对撞机时,它应该将其位置转换为原始位置。

我看不出有什么问题,我可以得到一些帮助吗?

P.S 当其他游戏对象也通过对撞机时,我会收到此错误。

NullReferenceException:对象引用未设置为对象的实例

【问题讨论】:

标签: c# unity3d virtual-reality steamvr


【解决方案1】:

如果对生成点使用变换,请记住在检查器菜单中设置它的值。

public Transform respawnPoint;

void OnTriggerEnter(Collider other)
{    
    if (other.CompareTag("Basketball"))
        other.transform.position = respawnPoint.position;
}

其他

public Vector3 respawnPoint = Vector3.zero;

void OnTriggerEnter(Collider other)
{    
    if (other.CompareTag("Basketball"))
        other.transform.position = respawnPoint;
}

【讨论】:

    【解决方案2】:
    private void OnTriggerEnter(Collider other){
    
        if(other.gameobject.tag=="Basketball"){
    
           other.gameobject.transform.position = respawnPoint;
        }
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多