繁体   English   中英

对象未设置为在构建中引用

[英]Object not set to reference in build

在编辑器中播放我的统一项目和播放其生成的版本之间,我存在一些差异。

在编辑器内部,当我玩游戏时,我的代码会在玩家击中目标位置时生成敌人,而目标位置会起作用。 敌人产生并做他们需要做的事。 但是,当我构建项目并运行它时,我的播放器碰到了相同的位置,但是什么也没有出现。

我以开发模式运行该项目,它声称我没有设置我认为有的参考。 我的意思是,如果它不起作用,为什么在编辑器中起作用?

为了解决这个问题,我重新导入了我正在使用的资产。 我重新制作了敌人的生成点。 但是我仍然遇到同样的问题。

有人遇到过吗? 如果是这样,当它在编辑器中而不是在内部版本中工作时,如何解决此问题?

这是我的生成函数:

public GameObject SpawnEnemies()
{
    Vector3 _position = new Vector3(transform.position.x, transform.position.y, transform.position.z);

    // instantiate particel system
  //  Instantiate(_particle, _position, Quaternion.identity);
    audio.PlayOneShot(_appearSound);

    _clone = (GameObject)Instantiate(_enemy, _position, transform.rotation);
    _ai = _clone.GetComponent<HV_BaseAI>();
    _ai._waypoints = _wayPoints;

    return _clone;
}

这由我的section类控制,该类具有以下功能:

   public List<GameObject> SpawnGroundEnemies()
{
    List<GameObject> groundObjectList = new List<GameObject>();

    for (int i = 0; i < _spawnPoints.Count; i++)
    {
        groundObjectList.Add(_spawnPoints[i].SpawnEnemies());
    }
    return groundObjectList;
}

依次由我的部分控制器控制:

       GameObject g, f;
    g = GameObject.FindGameObjectWithTag("SectionController");
    f = GameObject.FindGameObjectWithTag("SectionController");

    HV_SectionController tempSectionControllerGroundEnemies, tempSectionControllerFlyingEnemies;
    tempSectionControllerGroundEnemies = g.GetComponent<HV_SectionController>();
    tempSectionControllerFlyingEnemies = f.GetComponent<HV_SectionController>();

    _groundEnemiesRemaining = tempSectionControllerGroundEnemies._numberOfGroundEnemies.Count;
    _flyingEnemiesRemaining = tempSectionControllerFlyingEnemies._numberOfFlyingEnemies.Count;
    _enemiesRemaining = _groundEnemiesRemaining + _flyingEnemiesRemaining;

    if (!_moving)
    {
        if (_enemiesRemaining == 0)
        {
            MoveToNextSection();

        }

    }

    if (_moving)
    {
        if (Vector3.SqrMagnitude(_camera.transform.position - _camNavMesh.destination) < 5.0f)
        {
            _moving = false;


            // spawn the next set of enemies
            // need this for each section of the game
            // without this, no enemies will spawn
            if (CurrentSection == 1)
            {
                _numberOfGroundEnemies = _sections[0].SpawnGroundEnemies();
                _enemiesRemaining = _numberOfGroundEnemies.Count;
            }

            if (CurrentSection == 2)
            {
                _numberOfFlyingEnemies = _sections[1].SpawnFlyingEnemies();
                _enemiesRemaining = _numberOfFlyingEnemies.Count;
            }

            if (CurrentSection == 3)
            {
                _numberOfGroundEnemies = _sections[2].SpawnGroundEnemies();
                _enemiesRemaining = _numberOfGroundEnemies.Count;
            }
        }
    }
}

private void MoveToNextSection()
{
    if (_currentSection == _sections.Count)
    {
        _currentSection = 0;
    }
    _camNavMesh.SetDestination(_sections[_currentSection]._cameraTransform.position);
    _lookAtPoint.SetTimer(_sections[_currentSection]._cameraLookAtTarget.position);
    _moving = true;

    _currentSection += 1;
}

谁能看到我做错了什么?

更新资料

从发布此帖子开始,我尝试再次重新导入我的所有资产。 我什至创建了一个新项目,并以这种方式完成了新的构建,但是我仍然遇到同样的问题。 另外,为提供帮助,我进行了两次抓屏以显示我的意思。

首先,在统一编辑器中运行游戏: 在此处输入图片说明

如您所见,桥上有4个骨架。 当我在单位编辑器中点击“播放”时,这些人就会出现。

现在,当我构建项目时,这是相同的部分: 在此处输入图片说明

如您所见,没有敌人,但是在开发过程中,它表示我没有参考集。 在输出日志统一中,它给我以下消息:

NullReferenceException:对象引用未设置为C:\\ Users \\ sean.obrien \\ Desktop \\ Medieval Darkride Level \\ Assets_scripts \\ Enemy Scripts \\ HV_SpawnGroundEnemy.cs:37中的HV_SpawnGroundEnemy.SpawnEnemies()[0x00052]处的对象实例

在C:\\ Users \\ sean.obrien \\ Desktop \\ Medieval Darkride Level \\ Assets_scripts \\ Helper Scripts \\ HV_Section.cs:26中的HV_Section.SpawnGroundEnemies()[0x0000d]

在HV_SectionController.Update()在C:\\ Users \\ sean.obrien \\ Desktop \\ Medieval Darkride Level \\ Assets_scripts \\ Helper Scripts \\ HV_SectionController.cs:72中的[0x000c2]

同样,这仅出现在游戏的内置版本中。 当我在Unity中运行此程序时,一切正常。 老实说,我不知道为什么当它在编辑器中正常工作而在构建中无法正常工作时,为什么会出现此问题。

这里很可能有两种情况。 您可能尚未保存场景,或者在进行构建时未包括正确的场景。

如果是第一种情况,请保存场景。 如果是第二个,则需要在构建设置中选择合适的场景,然后重新构建。

说明:
您可能已将场景另存为其他文件,但是构建设置不会动态更新场景文件。 它仍然指向旧场景。 因此,您只需要删除旧场景并添加您正在使用的当前场景即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM