c# - 错误 CS1519 - 类、结构或接口成员声明中的令牌“浮动”无效
问题描述
我正在创建一个小行星射击游戏,我正在为班级做作业以增加分数。在 Asteroid 脚本中添加“scoreValue”和“playerScript”后,
我现在收到“错误 CS1519 - 类、结构或接口成员声明中的无效令牌‘浮动’”
这是我当前的错误代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class asteroidScript : MonoBehaviour
{
public float speed;
public Transform explosion;
private asteroidScript scriptAsteroid;
public int scoreValue;
public playerScript
//my screen size limits
float minX = -12.72f,
maxX = 12.72f,
minY = -11.89f,
maxY = 11.89f;
float startY, endY;
private int newScoreValue;
// Start is called before the first frame update
void Start()
{
startY = maxY + 3; //initializing start point for asteroid
endY = minY + -3; // initializing end point for asteroid
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
//check if I passed the bottom of the screen
if (transform.position.y < minY)
{
//function call
resetEnemy();
}
}
public void resetEnemy()
{
//Reset the position of the asteriods
Vector3 position = transform.position;
position.y = startY;
//randomly choose my x position
position.x = Random.Range(minX, maxX);
//put the asteroid at that position
transform.position = position;
}
void OnTriggerEnter(Collider other)
{
//I am going to check what I collided with
if (other.gameObject.tag == "Asteroid")
{
//Reposition the asteroid to the top of the game
//get the asteriod Script
scriptAsteroid = other.GetComponent<asteroidScript>();
//call the function to reset asteroid
scriptAsteroid.resetEnemy();
//create explosion
Instantiate(explosion, transform.position, transform.rotation);
//reset myself - the asteroid
resetEnemy();
}
//I am going to check if I collided with the player
if(other.gameObject.tag == "Player")
{
//create explosion
Instantiate(explosion, transform.position, transform.rotation);
//reset myself - the asteroid
resetEnemy();
//later I have to code the shield in :)
}
{
playerScript.AddScore(newScoreValue);
}
}
}
这是我的代码在进行更改之前没有错误的样子......
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class asteroidScript : MonoBehaviour
{
public float speed;
public Transform explosion;
private asteroidScript scriptAsteroid;
//my screen size limits
float minX = -12.72f,
maxX = 12.72f,
minY = -11.89f,
maxY = 11.89f;
float startY, endY;
// Start is called before the first frame update
void Start()
{
startY = maxY + 3; //initializing start point for asteroid
endY = minY + -3; // initializing end point for asteroid
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
//check if I passed the bottom of the screen
if (transform.position.y < minY)
{
//function call
resetEnemy();
}
}
public void resetEnemy()
{
//Reset the position of the asteriods
Vector3 position = transform.position;
position.y = startY;
//randomly choose my x position
position.x = Random.Range(minX, maxX);
//put the asteroid at that position
transform.position = position;
}
void OnTriggerEnter(Collider other)
{
//I am going to check what I collided with
if (other.gameObject.tag == "Asteroid")
{
//Reposition the asteroid to the top of the game
//get the asteriod Script
scriptAsteroid = other.GetComponent<asteroidScript>();
//call the function to reset asteroid
scriptAsteroid.resetEnemy();
//create explosion
Instantiate(explosion, transform.position, transform.rotation);
//reset myself - the asteroid
resetEnemy();
}
//I am going to check if I collided with the player
if(other.gameObject.tag == "Player")
{
//create explosion
Instantiate(explosion, transform.position, transform.rotation);
//reset myself - the asteroid
resetEnemy();
//later I have to code the shield in :)
}
}
}
我看不出出了什么问题,以及为什么“Float”现在被宣布为无效令牌。任何解决方案都是最有帮助的!谢谢!
- 贾斯汀
解决方案
你应该使用“;” 在声明“public playerScript”之后的第 9 行,这是一个常见的丢失分号我知道它很晚,但我希望其他人可以使用它
推荐阅读
- three.js - 将点从正方形更改为矩形
- r - 如何生成系列中的前 n 项:
- ruby-on-rails - Cron 工作在开发中的任何时候都不起作用
- html - 将 svg 元素包裹在圆圈中,而无需使用 javascript
- cplex - Cplex(小数位数)
- java - 滚动时无法单击列表视图中的按钮
- wpf - 是否有任何控件不是从 WPF 中的依赖对象派生的?
- python - 如何打破 python 中的 try 循环?
- android - 当我的应用程序既不在后台运行也不在前台运行时,如何让我的应用程序获取(事务性)SMS?
- javascript - ReactJS this.props.history.push 不存在!this.props 不包括历史