微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

磁铁通电 像地铁冲浪者一样不起作用

如何解决磁铁通电 像地铁冲浪者一样不起作用

我正在尝试让磁铁像地铁冲浪者一样通电。我的游戏是一个无尽的亚军游戏。当跑步者与磁铁发生碰撞时,硬币会被吸引向玩家。但它不起作用。跑步者与磁铁碰撞后,每次与硬币碰撞后,错误不断出现。

NullReferenceException: 未将对象引用设置为对象的实例 Coin.OnTriggerEnter(UnityEngine.Collider 其他)(在 Assets/Scripts/Coin.cs:37)

有人可以帮我吗?

磁铁脚本

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

public class Magnet : MonoBehavIoUr
{
// Start is called before the first frame update
 public GameObject coinDetectorObj;

public static bool iscoinDetectorObj;
// Start is called before the first frame update
void Start()
{

    coinDetectorObj = GameObject.FindGameObjectWithTag("coin detector");
    coinDetectorObj.SetActive(false);
}

private void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Player")
    {
        StartCoroutine(ActivateCoin());
        Destroy(transform.GetChild(0).gameObject);
        
    }
}

IEnumerator ActivateCoin()
{
    coinDetectorObj.SetActive(true);
    yield return new WaitForSeconds(0.5f);
    coinDetectorObj.SetActive(false);
}
    }

硬币脚本

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

public class Coin : MonoBehavIoUr
{

public Transform playerTransform;
public float moveSpeed = 17f;

CoinMove coinMoveScript;

void Start()
{
    playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
    coinMoveScript = gameObject.GetComponent<CoinMove>();
}

void Update()
{
    transform.Rotate(130 * Time.deltaTime,0);
    
}

private void OnTriggerEnter(Collider other)
{
    if (other.tag == "Player")
    {
        FindobjectOfType<AudioManager>().PlaySound("PickUpCoin");
        PlayerManager.numberOfCoins += 1;
        Destroy(gameObject);

    }

    if (other.gameObject.tag == "coin detector")
    {
        coinMoveScript.enabled = true;

    }
}

}

CoinMove 脚本

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

public class CoinMove : MonoBehavIoUr
{
Coin coinScript;

// Start is called before the first frame update
void Start()
{
    coinScript = gameObject.GetComponent<Coin>();
}

// Update is called once per frame
void Update()
{
    transform.position = Vector3.Movetowards(transform.position,coinScript.playerTransform.position,coinScript.moveSpeed * Time.deltaTime);
}

private void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Player")
    {
        //Add count or give points etc etc.
        Destroy(gameObject);
    }
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。