- Home /
With the help of another pair of eyes looking over my code I found the problem, I explained it in a comment
Loading scenes from my menu breaks a script that usually works...
Okay, first of all sorry for the title, I just really don't know how to describe the problem in a chort manner.
The thing is, I have multiple levels in a tower-defense-like game and inside of each level I have GameObject WorldSpawner with a script enemySpawn, responsible for, you guessed it, spawning the enemies.
The WorldSpawner also has a script called SceneChanger that changes my current scene by pressing the page up or page down key, loading the next or previous scene respectiveley. This was for testing purposes, works perfectly fine and I am currently working on the UI, including a main- and levelselection-menu.
But for whatever reason, when I load a level via the scripts for my menu buttons the enemies won't spawn and I can't seem to figure out what the big difference between the two scene-loading scripts is that breaks it.
I'll post the scripts down here and would really appreciate any help you guys n' girls can offer. :)
This is the SceneChanger-Script:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class SceneChanger : MonoBehaviour
{
private int currentScene;
// Use this for initialization
void Start()
{
currentScene = SceneManager.GetActiveScene().buildIndex;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("page up"))
{
SceneManager.LoadScene(currentScene + 1);
}
else if(Input.GetKey("page down"))
{
SceneManager.LoadScene(currentScene - 1);
}
}
}
This is the LevelselectionMenu-Script:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LevelselectionMenu : MonoBehaviour
{
public Button level1But;
public Button level2But;
public Button level3But;
public Button level4But;
public Button level5But;
public Button backBut;
void Start()
{
level1But = level1But.GetComponent<Button>();
level2But = level2But.GetComponent<Button>();
level3But = level3But.GetComponent<Button>();
level4But = level4But.GetComponent<Button>();
level5But = level5But.GetComponent<Button>();
backBut = backBut.GetComponent<Button>();
}
public void level1Press()
{
SceneManager.LoadScene("Scenes/Scene1");
}
public void level2Press()
{
SceneManager.LoadScene("Scenes/Scene2");
}
public void level3Press()
{
SceneManager.LoadScene("Scenes/Scene3");
}
public void level4Press()
{
SceneManager.LoadScene("Scenes/Scene4");
}
public void level5Press()
{
SceneManager.LoadScene("Scenes/Scene5");
}
public void backButPress()
{
print("backButPressed");
SceneManager.LoadScene("Scenes/MainMenu");
}
}
And finally, here's the EnemySpawn-Script(what's needed for the first level anyways, sorry this whole thing has become so long but I wanted to give you lots of information):
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System;
public class EnemySpawn : MonoBehaviour
{
private GameObject enemy;
private int enemyType;
private int laneID;
private int goodOrBad;
private int enemiesSpawned;
private int waveSize;
private int getZ;
private float zKoordinate;
private float lane;
private string currentScene;
private Vector3 enemySpawnPoint;
private DateTime firstStamp;
private DateTime secondStamp;
private bool timePassed;
// Use this for initialization
void Start()
{
currentScene = SceneManager.GetActiveScene().name;
Debug.Log(currentScene);
switch (currentScene)
{
case "Scene1":
waveSize = 20;
spawnWave1();
break;
case "Scene2":
waveSize = 30;
spawnWave2();
break;
case "Scene3":
waveSize = 30;
spawnWave3();
break;
case "Scene4":
waveSize = 35;
spawnWave4();
break;
case "Scene5":
waveSize = 50;
spawnWave5();
break;
}
enemiesSpawned = 0;
firstStamp = DateTime.Now;
}
// Update is called once per frame
void Update()
{
secondStamp = DateTime.Now;
if (secondStamp >= firstStamp.AddSeconds(1.0) && enemiesSpawned < waveSize)
{
switch (currentScene)
{
case "Scene1":
spawnWave1();
break;
case "Scene2":
spawnWave2();
break;
case "Scene3":
spawnWave3();
break;
case "Scene4":
spawnWave4();
break;
case "Scene5":
spawnWave5();
break;
}
firstStamp = DateTime.Now;
}
else if (enemiesSpawned > waveSize && GameObject.FindGameObjectWithTag("Enemy") == null)
{
Debug.Log("Sie haben gewonnen!");
//AppHelper.Quit();
}
}
public void spawnWave1()
{
laneID = (int)UnityEngine.Random.Range(1, 5);
switch (laneID)
{
case 1:
lane = -15.0f;
break;
case 2:
lane = -5.0f;
break;
case 3:
lane = 5.0f;
break;
case 4:
lane = 15.0f;
break;
}
enemy = Resources.Load("Enemies/Asteroid", typeof(GameObject)) as GameObject;
enemy = Instantiate(enemy);
enemySpawnPoint = new Vector3(lane, 11.0f, 70.0f);
enemy.transform.position = enemySpawnPoint;
enemiesSpawned++;
}
}
Okay, so like a complete moron I didn't notice that I set the currentScene string to "Scenes/Scene1" etc. and then checked wether or not the name was "Scene1" etc in my spawn-script which of course isn't the case...
Follow this Question
Related Questions
basic Saving and Loading 1 Answer
Spawning Coins infront of the player 2 Answers
Start menu gui wont load scene 2 Answers