- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
Unity 新手。
所以我创建了一个简单的 Guzzle 闪光粒子动画,当玩家靠近他时应该在敌人的枪上显示它,模拟没有实际子弹的射击。但是,我在这部分 muzzleFlash.Play();
中得到了一个空引用异常,我相信这是因为我实际上并没有使用我拥有的代码在启动函数中获取 Guzzle 闪光组件,实际上我知道是在进入 Debug模式后我发现的吗?我很难弄清楚如何访问该组件。下面是我的代码,我还张贴了我的层次结构的图片。提前致谢。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StaticShootingEnemy : MonoBehaviour
{
[SerializeField] private float _range = 12f;
private Transform _player;
private bool _alive;
private float _distance;
private ParticleSystem muzzleFlash;
// Use this for initialization
void Start()
{
_player = GameObject.Find("Player").transform;
_alive = true;
muzzleFlash = (ParticleSystem)this.gameObject.GetComponent("muzzleFLash");
}
// Update is called once per frame
void Update()
{
_distance = Vector3.Distance(this.transform.position, _player.transform.position);
if (_alive && _distance < _range)
AttackPlayer();
}
private void AttackPlayer()
{
//Turning enemy to look at player
transform.LookAt(_player);
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.SphereCast(ray, 0.75f, out hit))
{
//TODO: Fix enemy shooting fast when gettting close to him.
GameObject hitObject = hit.transform.gameObject;
if (hitObject.GetComponent<PlayerController>())
{
muzzleFlash.Play();
Debug.Log("Player Hit!");
}
else
muzzleFlash.Stop();
}
}
public void SetAlive(bool alive)
{
_alive = alive;
}
}
最佳答案
您可能有一个对象“muzzleFlash”作为子对象来反对您的脚本。因此,在这种情况下,您最好引用名为 muzzleFlash 的 ParticleSystem 对象。
[SerializeField] private ParticleSystem muzzleFlash; // drag and drop your ParticleSystem muzzleFlash in inspector
或者至少你可以找到像这样的 muzzleFlash
GameObject muzzleFlashObj = GameObject.Find("muzzleFlash");
ParticleSystem muzzleFlash = muzzleFlashObj.GetComponent<ParticleSystem>();
在您的情况下,它为空,因为该对象上可能没有名为 MuzzleFlash 的组件。您要获取的组件是 ParticleSystem。
关于c# - 了解Unity的GameObject.Find()、GetComponent()和对象回收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52824146/
我对在编写 Unity 脚本时继承自 MonoBehaviour 的 gameObject 的使用有疑问。 在一些教程中,例如制作缩放的健康条,我们检索健康条的原始比例如下: originalScal
我有一个名为 trigger 的无敌 GameObject,当我的 Hero 与它碰撞时,一盏枝形吊灯会掉落。我想给枝形吊灯一个 Rigidbody,这样它就会掉落,您可以与它碰撞并使用它。 如果你能
我正在使用 Unity 开发一款游戏,其中所有带有特定标签的 Gameobjects 都会相当规律地消失/重新出现(平均每 10 秒一次)。我使用 GameObject.FindGameObjects
public void FindClosestEnemy() { List pList = GameObject.FindGameObjectsWithTag("Player");
我有两个像这样的 GameObject: public class GOA : MonoBehaviour { void Start() { ... do something
概览 考虑一个简单的硬币收集游戏。有两个脚本,CollectableItem 和 ItemVault。 CollectableItem 有一个 value 属性,它告诉项目的值(value),它被添加
我试图找出我的游戏对象正在被我的游戏中的哪个脚本破坏。 到目前为止,我尝试打印堆栈,但那里没有提到销毁游戏对象的脚本的详细信息 代码:Debug.Log(UnityEngine.StackTraceU
在 Unity3D 序列化 GameObject 之前是否有任何回调(或任何其他方式)通知? 我需要类似于 OnSerializeNetworkView 的东西但对于常规 GameObject。 有用
我正在使用 Unity 制作游戏。我在随机位置实例化 (circle) GameObjects 并且效果很好。我想要做的是仅在不会与其他 GameObjects 重叠的位置实例化。我在互联网上找到的解
我遇到了一个小问题。 我制作了一个 3D 立方体并设置了 GoogleVR SDK,以便在 VR 屏幕的中点进入立方体时使用事件触发器。发生这种情况时,将触发我的自定义方法 LookAtCube()。
我想获取某个文件夹中的所有预制件,然后将其转换为列表 我的代码: this.list = (List) (Resources.LoadAll("items")).ToList(); // this c
所以我的游戏世界中有三个不同的“旁观者”,每个都有一个附加的“旁观者”脚本,并且我的 UI 中有一个“旁观者对话”元素。这个想法是,当玩家进入任何旁观者的范围时,会显示从数据库中随机选择的文本,但对于
我正在编写我的游戏 Boss 行为代码,在战斗的最后阶段,Boss 应该向玩家冲锋,然后回到原来的位置。等待 5 秒,然后执行相同的操作。 我尝试使用协同程序和 Vector2.MoveTowards
总结如下:我想从 OnTriggerEnter2D 事件中找到参与碰撞的两个对撞机。我该怎么做? 我有两个游戏对象。两者都有一个碰撞器和一个触发器。 在对象 A 上,它被触发器包围。在对象 B 上,触
我目前有一个脚本应该实例化一个粒子系统并在一定时间后销毁它,但是在实例化对象之后,出现此错误代码: MissingReferenceException: The object of type 'Par
我目前有一个脚本应该实例化一个粒子系统并在一定时间后销毁它,但是在实例化对象之后,出现此错误代码: MissingReferenceException: The object of type 'Par
Unity 对象不在中心旋转。直到一个小时都很好,我不相信我更改了代码或任何东西。我也试过弄乱左上角的枢轴/中心按钮,但没有任何帮助。游戏对象没有在中间旋转。 旋转器代码: using UnityEn
我很难将一些游戏对象链接在一起以“跟随领导者”。 这就是我想要完成的: 有“头”对象的地方,它会拉动 body 对象。像蛇或蜈蚣。 然而,这是正在发生的事情: 它们都有点跟随头部,但它们似乎作为一个整
假设我的脚本定义如下。 public GameObject _GameObject; private void Start() { [![enter image description here][1
所以基本上我是在尝试将一群 child 添加到游戏对象中。父游戏对象称为 SnakeHead,而所有其他游戏对象称为 SnakeBodyParts public class GameControlle
我是一名优秀的程序员,十分优秀!