首页 > 解决方案 > 错误 CS0106:修饰符 'private' 对此项目无效 Unity 中的 C# 错误

问题描述

我不断收到此错误(CS0106:修饰符 'private' 对此项目无效)并且可以使用一些帮助。我正在尝试为我的游戏制作一个随机对象生成器,但由于我仍然是一个新手编码器,我似乎无法弄清楚如何解决这个问题。你能帮忙的话,我会很高兴

这是我使用的代码:

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

public class deployAsteroids : MonoBehaviour
{

public GameObject asteroidPrefab;
public float respawnTime = 1.0f;


void Start()
{
    screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(screenBounds.x, screenBounds.y, Camera.main.transform.position.z));
    StartCorountine(asteroidWave());
    
    private void spawnEnemy()
    {
        GameObject a = Instantiate(asteroidPrefab) as GameObject;
        a.transform.position = new Vector2(Random.Range(-screenBounds.x, screenBounds.x), screenBounds.y * -2);
    }
    IEnumerator astroidWave()
    {
        while (true)
        {
            yield return new WaitForSeconds(respawnTime);
            spawnEnemy();
        }
    }
}

标签: c#unity3d

解决方案


每当您遇到编译器错误时,您首先应该考虑的是在搜索引擎中查找错误代码。从CS0106 开始提供规则。第四点很清楚。

本地函数上不允许使用访问修饰符。本地函数始终是私有的。

你有两个选择:

  1. 将方法移到父方法 ( Start()) 之外。如果将在多个地方使用,这是典型的场景。
  2. 删除修饰符private

推荐阅读