如何从游戏对象访问可编写脚本的对象

问题描述 投票:0回答:1

我正在尝试从可编写脚本的对象访问数据,但是由于某种原因,列表从一种方法更改为另一种方法。

在我的启动方法中,我打印的枪支清单的计数是2。这是正确的,当前其中有两个数据类型为Firearm的可编写脚本的对象。

void Start()
    {
        GetGuns();
        cam = Camera.main;

        // Now we must select are weapon
        SelectWeapon();
    }
    void GetGuns()
    {
        // This finds all the weapons which will have a tag of Gun
        this.weapons = new List<GameObject>(GameObject.FindGameObjectsWithTag("Gun"));
    }

播放器射击时不,调用此方法ShootGun()。在此方法内,我尝试使用gunList,但它给了我一个空异常。

    public void Shoot()
    {
        Debug.Log("This is the count of GunList: " + gunList.Count);
        GetGuns();
    }

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

这是它给我的错误。我不知道它如何从一种方法变为另一种方法,没有什么应该影响它。

这就是我想要做的。我要捕获活动武器,然后使用所选武器作为索引在枪支列表中引用此武器。我要从这里开始实例化效果/枪声。

这是对WeaponManager类的引用

GameObject.Find("WeaponHolder").GetComponent<WeaponManager>().Shoot();

这是在播放器控制器类内部调用它的方法。

    void Attack()
    {
        attacking = true;
        Invoke("EndAttack", 1.0f);

        GameObject.Find("WeaponHolder").GetComponent<WeaponManager>().Shoot();
    }

这是整个课程,可能会有所帮助。

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

public class WeaponManager : MonoBehaviour
{
    // This is so we know what weapon they currently have on them.
    public int selectedWeapon = 0;
    public List<Firearm> gunList;
    public List<GameObject> weapons;

    private bool isReloading = false;
    private int currentAmmo = -1;
    private float nextShot = 0f;

    private Camera cam;

    // Start is called before the first frame update
    void Start()
    {
        GetGuns();
        cam = Camera.main;

        // Now we must select are weapon
        SelectWeapon();
    }
    void GetGuns()
    {
        // This finds all the weapons which will have a tag of Gun
        this.weapons = new List<GameObject>(GameObject.FindGameObjectsWithTag("Gun"));
    }
    // Update is called once per frame
    void Update()
    {
        if (gunList.Count > 0)
        {
            Debug.Log("There is currently: " + gunList.Count);
        }

        int previousWeapon = selectedWeapon;
        // Scroll wheel up
        if (Input.GetAxis("Mouse ScrollWheel") > 0f)
        {
            Debug.Log("Getting into scroll wheel up");
            if(selectedWeapon >= weapons.Count - 1)
            {
                selectedWeapon = 0;
            }
            else
            {
                selectedWeapon++;
            }
        }
        // Scroll wheel down
        if (Input.GetAxis("Mouse ScrollWheel") < 0f)
        {
            Debug.Log("Getting into scroll wheel down");
            if (selectedWeapon <= 0)
            {
                selectedWeapon = gunList.Count - 1;
            }
            else
            {
                selectedWeapon--;
            }
        }
        // This now checks if they changed indexes
        if (previousWeapon != selectedWeapon)
        {
            // If they did select new weapon.
            SelectWeapon();
        }
    }
    public void Shoot()
    {
        Debug.Log("This is the count of GunList: " + gunList.Count);
        GetGuns();
    }

    void SelectWeapon()
    { 
        int i = 0;
        foreach (GameObject weapon in weapons)
        {
            if (i == selectedWeapon)
            {
                weapon.SetActive(true);
            }
            else
            {
                weapon.SetActive(false);
            }
            i++;
        }

    }
}

c# unity3d nullreferenceexception
1个回答
0
投票

嗨,非常感谢您发表评论并尝试提供帮助的所有人。实际上,正是我所说的这种方法给我带来了问题。

似乎我解决了这个问题,原来是我这么做的

WeaponManager wpnMngr = new WeaponManager();

这不是正确的方法,您应该这样做

GameObject.Find("WeaponHolder").GetComponent<WeaponManager>().Shoot();

再次感谢大家的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.