【问题标题】:How do I make a local variable in another function usable? [duplicate]如何使另一个函数中的局部变量可用? [复制]
【发布时间】:2021-10-01 02:19:14
【问题描述】:

现在我希望实例化的子弹在与墙壁碰撞时销毁,但 Visual Studio 表示 OnTriggerEnter2D 函数的当前上下文中不存在“子弹”。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace work.working.worked
{
    public class Shooting : MonoBehaviour
    {
        public static bool firing;
        public Transform firepoint;
        public GameObject bulletprefab;
        public float bulletspeed;
        // Update is called once per frame
        void Update()
        {
            if (firing == false)
            {
                if (Movement.ismoving == false)
                {
                    firing = true;
                    StartCoroutine(Shoot());
                }
            }
        }
        IEnumerator Shoot()
        {
            GameObject bullet = Instantiate(bulletprefab, firepoint.position, firepoint.rotation);
            Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
            rb.AddForce(firepoint.up * bulletspeed, ForceMode2D.Impulse);
            yield return new WaitForSeconds(0.5f);
            firing = false;
        }
        private void OnTriggerEnter2D(Collider2D collision)
        {
            Destroy(bullet);
        }
    }
}

【问题讨论】:

  • 如果你想在其他地方使用它,它不应该是局部变量。
  • 它不会做你想做的事,因为你想在当子弹碰撞时摧毁子弹,而不是在这个对象产生子弹碰撞 ... => 在子弹预制件上放置一个专用组件来跟踪 OnTriggerEnter ...

标签: c# unity3d


【解决方案1】:

您不能这样做,也不能更改方法签名并将变量作为方法参数传递,因为它是一个统一事件函数,所以它必须保持原样。相反,把它变成一个字段。

using System.Collections.Generic;
using UnityEngine;
namespace work.working.worked
{
    public class Shooting : MonoBehaviour
    {
        public static bool firing;
        public Transform firepoint;
        public GameObject bulletprefab;
        public float bulletspeed;
    private GameObject bullet;
        // Update is called once per frame
        void Update()
        {
            if (firing == false)
            {
                if (Movement.ismoving == false)
                {
                    firing = true;
                    StartCoroutine(Shoot());
                }
            }
        }
        IEnumerator Shoot()
        {
            bullet = Instantiate(bulletprefab, firepoint.position, firepoint.rotation);
            Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
            rb.AddForce(firepoint.up * bulletspeed, ForceMode2D.Impulse);
            yield return new WaitForSeconds(0.5f);
            firing = false;
        }
        private void OnTriggerEnter2D(Collider2D collision)
        {
            Destroy(bullet);
        }
    }
}

【讨论】:

  • 我在这里把子弹变成了一个字段,而不是一个局部变量。这是你必须做的。
  • 由于某种原因,子弹在碰撞时没有破坏
  • 确保子弹和碰撞对象都有碰撞器,并且子弹上的碰撞器已选中“isTrigger”框。
  • 我的男人,你是救生员。我希望你记住,在你每天晚上睡觉之前,知道你已经解决了一个随机的人在互联网上超过 2 天的问题。
  • 哈哈,我很高兴。你可以这样做,虽然哈哈stackoverflow.com/help/someone-answers顺便说一句,如果你愿意,你可以加入我们的不和谐,这是一个适合初学者的地方discord.gg/yMuQfSQvEt
猜你喜欢
  • 2017-12-22
  • 1970-01-01
  • 2019-03-30
  • 1970-01-01
  • 2016-08-05
  • 2015-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多