【问题标题】:How can I redirectveriable veriable from object to my haracter如何将变量变量从对象重定向到我的角色
【发布时间】:2022-12-16 02:29:18
【问题描述】:

我有 2 个脚本,一个在播放器上:

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

public class PlayerPositionCorrection : MonoBehaviour
{

    Transform _playerTransform;
    public float _xAxys;
    public float _newXAxys;

    public GameObject _changerPlayerPosition;

    private void Start()
    {
        _playerTransform = GetComponent<Transform>();
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "ChangePlayerPosition")
        {
            float _newXAxys = this.GetComponent<ChangePositionOn>()._newPostion;
            
        }
    }

    private void LateUpdate()
    {
        if (transform.position.z != 0)
        {
            transform.position = new Vector3(_xAxys, _playerTransform.position.y, _playerTransform.position.z);
        }
    }

第二个对象:

public class ChangePositionOn : MonoBehaviour
{

    public float _newPostion = 5;

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

我使用 Unity 2022.1.19f1 和 C#。

谢谢你的医治, 迈克尔

我想在我的游戏中有几个对象,当玩家与它们发生碰撞时会改变 x 轴上的位置。

不幸的是,每次我收到此错误消息时:

NullReferenceException: Object reference not set to an instance of an object
PlayerPositionCorrection.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/PlayerPositionCorrection.cs:23)

【问题讨论】:

  • 许多拼写错误使它难以阅读:更正拼写错误。
  • 该异常表明您尝试对其进行 GetComponent 操作的对象没有该组件。检查编辑器是否存在。

标签: c# unity3d nullreferenceexception


【解决方案1】:
  1. 检查 ChangePositionOn 脚本是否附加到播放器 游戏对象。
  2. 删除函数中的 float 声明,因为它使它成为 局部变量
  3. 用这个替换你的函数
    private void OnTriggerEnter(Collider other)
            {
                if (other.CompareTag("ChangePlayerPosition"))
                {
                    Debug.Log("Check if Collided With : "+ other.gameObject.name);
                    _newXAxys = this.GetComponent<ChangePositionOn>()._newPostion;
                    Debug.Log("New XAxys value : "+ _newXAxys );
                }
            }
    

【讨论】:

  • 好的,错误没有出现,但是在与对象碰撞后应该改变 xaxis 但是没有任何改变。
  • 阅读我的编辑并在失败时发送截图
  • 它的作品,但并不像预期的那样。如果我展示我想要实现的目标,也许会更容易。 imgur.com/5zxRI0u 现在我们将数据 _newXAxys 保存到播放器元素中。然而,这个数据应该在带有碰撞器的第二个元素中(与玩家预制无关)。当玩家与这个元素发生碰撞时,应该获取这个数据(在我们的例子中是浮点数)并改变位置(已经有效的)。
【解决方案2】:

制作一个新脚本 PositionCorrector 并将其附加到 Collider GameObject :

using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class PositionCorrector : MonoBehaviour
  {
  //this should be attached to Collider GameObject
  
     //this is the X value you want to change to, each collider can have a different number.
      public float _newXAxys=4;

      private void OnTriggerEnter(Collider other)
      {
          if (other.CompareTag("PlayerTag"))
          {
              //changing the x axys of the player position
              other.transform.position = new Vector3(_newXAxys, other.transform.position.y, other.transform.position.z);
              
          }
      }
  }

【讨论】:

  • 伙计们它的工作:)对于那些也像我一样想使用这个脚本的人请记住改变PlayerPositionCorrection:private void OnTriggerEnter(Collider other){if(other.CompareTag(“ChangePlayerPosition”)){Debug.Log(“Check如果碰撞:" + other.gameObject.name); _newXAxys = other.GetComponent<PositionCorrector>()._newXAxys; Debug.Log("新 XAxys 值:" + _newXAxys);谢谢你的帮助!你太棒了:)
【解决方案3】:

我对此还有其他问题。为什么当我尝试向这个脚本 PositionCorrector 添加一个未来时角色/玩家与对象碰撞并按下任意键(为此目的可以是'P')然后改变他的位置。但是,如果我碰撞角色,请不要等待按键立即改变位置。

private void OnTriggerEnter(Collider other)
{
    if (Input.GetKeyDown("p") && other.CompareTag("Player"))
    {
        _playerPostion = other.transform.position;
        _playerPostion = new Vector3(_newXAxys, _playerPostion.y, _playerPostion.z);
    }

}

【讨论】:

    猜你喜欢
    • 2011-08-09
    • 2014-10-31
    • 2011-05-23
    • 2021-10-08
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    相关资源
    最近更新 更多