c# - 错误 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();
}
}
}
解决方案
每当您遇到编译器错误时,您首先应该考虑的是在搜索引擎中查找错误代码。从CS0106 开始提供规则。第四点很清楚。
本地函数上不允许使用访问修饰符。本地函数始终是私有的。
你有两个选择:
- 将方法移到父方法 (
Start()
) 之外。如果将在多个地方使用,这是典型的场景。 - 删除修饰符
private
。
推荐阅读
- prolog - 叶([X,[],[]],[X])。如何获取树的叶子列表?
- c++ - 切换枚举类的非枚举值
- php - 在php中的图像中使用if语句时出错
- angular - 测试时如何创建 Angular 组件的新实例?
- javascript - 令牌过期时,Axios 拦截器未返回登录屏幕
- flutter - 如何在颤动中填充水平空间?
- javascript - Spring + Thymeleaf + JS,如何绑定元素?
- excel - Excel 查询 - 检查两列中的重复项并返回结果
- r - 用单行替换 tibble 中的行集
- dockerfile - 使用 Dockerfile 安装 Airflow Openshift