• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 29, 2016 at 04:10 PM by SvenFriedrich for the following reason:

With the help of another pair of eyes looking over my code I found the problem, I explained it in a comment

avatar image
Question by SvenFriedrich · Jun 29, 2016 at 05:43 PM · menuloadingspawningload level

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 t$$anonymous$$ng 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. T$$anonymous$$s 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. :)

T$$anonymous$$s is the SceneChanger-Script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class SceneChanger : MonoBehaviour
 {
     private int currentScene;
     // Use t$$anonymous$$s 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);
         }
     }
 }

T$$anonymous$$s 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 t$$anonymous$$s whole t$$anonymous$$ng 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 t$$anonymous$$s 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++;
     }
 }
 
 



Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SvenFriedrich · Jun 29, 2016 at 04:08 PM 0
Share

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...

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How come when I restart my level it doesn't work? 2 Answers

How to make a load screen with a load bar 1 Answer

Reloading Level problem 0 Answers

Spawning in a specified location 2 Answers

Spawning Player After Player Choice. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges