Я создаю игру со стрельбой по астероидам и делаю домашнюю работу для класса, чтобы добавить счет.После добавления «ScoreValue» и «playerScript» в сценарий Asteroid,
теперь я получаю «Ошибка CS1519 - Недопустимый токен« float »в объявлении члена класса, структуры или интерфейса»
Вот мойтекущий код с ошибкой:
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» теперь объявлен недействительным токеном.Любые решения будут наиболее полезными!Спасибо!