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

时间:2013-04-26 10:22:48

标签: c# unity3d

我在编辑器中播放我的Unity项目和播放它所构建的构建之间存在一些差异。

在编辑器中,当我玩我的游戏时,我的代码会在我的玩家击中目标点时生成敌人,这样可行。敌人产生并做他们需要做的事情。但是,当我构建项目并运行它时,我的播放器会击中相同的位置,但什么都没有出现。

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

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

有没有人曾见过这个? 如果是这样,我如何解决这个问题,当它在编辑器内部工作时,而不是在构建中?

这是我的产卵功能:

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;
}

任何人都可以看到我做错了吗?

更新

自从这篇文章以来,我已经尝试过再次重新导入我的所有资产。我甚至创建了一个新项目并以这种方式完成了新的构建,但我仍然遇到同样的问题。另外,为了提供帮助,我采取了两个屏幕抓取来展示我的意思。

首先,在统一编辑器中运行游戏: enter image description here

如您所见,桥上有4个骷髅。当我在统一编辑器中进行游戏时,会出现这些人。

现在,当我构建项目时,这是相同的部分: enter image description here

正如你所看到的,没有敌人,但是当它是一个开发版本时,它说我没有参考集。在输出日志统一生成中,它给了我这样的信息:

  

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

     

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

     

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

同样,这只出现在游戏的内置版本中。当我在Unity内部运行时,一切正常。老实说,当它在编辑器中正常工作但在构建中没有工作时,我不知道为什么会出现这个问题。

1 个答案:

答案 0 :(得分:0)

这里最有可能发生两起案件。您尚未保存场景,或者在构建时未包含正确的场景。

如果是第一种情况,只需保存场景即可。如果是第二个,那么您需要在构建设置中选择适当的场景,然后重建。

<强>解释
您可能已将场景另存为其他文件,但构建设置不会动态更新场景文件。它仍然指向旧场景。所以你只需要移除旧场景并添加你正在使用的当前场景。