Been making a Subway Surfer Clone to get into using unity. Stumbled into an cs1513 error i can’t resolve, checked the whole code for missing braces but I can’t find any. I also never created a function that returns a value, so I might be missing smth in that context.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
public GameObject[] obstaclePrefab;
// Base Speed modified over Time to increase difficulty
public float speed;
public float increaseDifficultyTime;
private float increaseDifficultyStart;
// Define minimum distance percentage between spawnable Objects ( will be used in relation to speed)
private float[] spawnTime = {0, 0, 0};
private float[] spawnLane = {0, 1, 2};
private int randomObstacle;
private float[] obstacleSpawnHeight = {1f, 2.1f, 2f, 2.2f};
private Vector3 spawnPos;
// Minimum time between different object Spawns, train lengths etc.
public float minSpawnCooldown = 1f;
private int trainLength;
private float trainCarOffset = 15.5f;
private float trainCarTimeOffset = 0.775f;
public float[] trainStillOnLane = {0, 0, 0};
// Game Over check variables
public PlayerController playerControllerScript;
// Start is called before the first frame update
void Start()
{
playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();
increaseDifficultyStart = increaseDifficultyTime;
}
// Update is called once per frame
void Update()
{
if (playerControllerScript.gameOver == false) {
createObstacle();
}
if (Time.time > increaseDifficultyStart) {
increaseDifficulty();
}
}
// Creates an Obstacle
void createObstacle() {
foreach (int lane in spawnLane) {
if (Time.time > spawnTime[lane]) {
// Choose a Random Obstacle with an addition of 1 to obstaclePrefabs length
randomObstacle = Random.Range(0, obstaclePrefab.Length + 3);
// Adds the ability to not add an obstacle
if (randomObstacle < 4) {
// If the Obstacle is a train car, get a random number between 1 and 3 to decide the trains length.
if (randomObstacle == 3 && checkLanesForTrains() == true) {
// Decide on the Length of the train
trainLength = Random.Range(1, 4);
for (int i = 0; i < trainLength; i++) {
spawnPos = new Vector3 (transform.position.x + (trainCarOffset * i), obstacleSpawnHeight[randomObstacle], (spawnLane[lane] - 1f) * 3);
Instantiate(obstaclePrefab[randomObstacle], spawnPos, transform.rotation);
}
// Set the spawntime on the lane and the value to check if the lane is still occupied for the checkLanesForTrains function
spawnTime[lane] = Time.time + (trainLength * trainCarTimeOffset) + minSpawnCooldown;
trainStillOnLane[lane] = spawnTime[lane];
} else {
spawnPos = new Vector3 (transform.position.x, obstacleSpawnHeight[randomObstacle], (spawnLane[lane] - 1f) * 3);
Instantiate(obstaclePrefab[randomObstacle], spawnPos, transform.rotation);
spawnTime[lane] = Time.time + minSpawnCooldown;
}
}
}
}
}
// Checks if two lanes are currently occupied by trains and returns false if this is the case
public bool checkLanesForTrains() {
private int occupied = 0;
foreach (int trainLane in spawnLanes) {
if (Time.time < trainStillOnLane[trainLane]) {
occupied ++;
}
}
if (occupied > 1) {
return (false);
} else {
return (true);
}
}
void increaseDifficulty() {
speed *= 1.1f;
increaseDifficultyStart = Time.time + increaseDifficultyTime;
}
}
These are The Errors I get:
