首页 > 解决方案 > 错误 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”现在被宣布为无效令牌。任何解决方案都是最有帮助的!谢谢!

标签: c#interfacedeclaration

解决方案


你应该使用“;” 在声明“public playerScript”之后的第 9 行,这是一个常见的丢失分号我知道它很晚,但我希望其他人可以使用它


推荐阅读