Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Monty-Monster · Jun 30, 2019 at 09:46 AM · restartrestart game

Restarting My Level Is Doubling The Game Objects In My Scene, But Why?

So to put it simply, I am making a game where you run around a maze and collect gems. To add a twist to this game I added traps in the game, one of them being holes that when you fall into restart the level. The problem is that when the level restarts, the number of gems in the maze doubles! (There are 15 gems in the first level and every time it restarts it adds 15) I don't know what is causing this but I don't want to take out a whole part of the game because of this glitch. Also, here's the level restarting script and the gem script in case I messed something up in one of them:

//Restarting Script//

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour { bool GameHasEnded = false; public float RestartDelay = 0f;

 public void EndGame()
 {
     if (GameHasEnded == false)
     {
         GameHasEnded = true;
         //Debug.Log("GameOver");
         Invoke("Restart", RestartDelay);
     }
 }

 public void Restart()
 {
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
 }

}

//Gem Script//

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CollectObjects : MonoBehaviour { public static int objects = 0; public AudioSource ding; // Use this for initialization void Awake() { objects++; }

 // Update is called once per frame
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         objects--;
         gameObject.SetActive(false);
         ding.Play();
     }
 }

}

//Number Of Gems Left Script//

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class ObjectsLeft: MonoBehaviour { //public string nextLevel; public GameObject Trophy; public GameObject Trigger; public GameObject Trigger2; GameObject objUI; // Use this for initialization void Start() { objUI = GameObject.Find("ObjectNum"); } // Update is called once per frame void Update() { objUI.GetComponent().text = CollectObjects.objects.ToString(); if (CollectObjects.objects == 0) { //Application.LoadLevel(nextLevel); //Destroy(objToDestroy); Trophy.SetActive(true); Trigger.SetActive(true); Trigger2.SetActive(true); objUI.GetComponent().text = "All objects collected."; } } }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AaronBacon · Jun 30, 2019 at 10:12 AM

I think it's because you're using Application.LoadLevel. Try switching to SceneManager.LoadScene

 using UnityEngine.SceneManagement;
 
     void Update()
     {
         objUI.GetComponent().text = CollectObjects.objects.ToString();
         if (CollectObjects.objects == 0)
         { //
             SceneManager.LoadScene(nextLevel);
             Destroy(objToDestroy);
             Trophy.SetActive(true);
             Trigger.SetActive(true);
             Trigger2.SetActive(true);
             objUI.GetComponent().text = "All objects collected.";
         }
     }


Be aware that the scene needs to be added to the build in the build settings, and can then be accessed by name or by ID. Also make sure you have UnityEngine.SceneManagement added to your usings

I am just going off other old questions like this btw, so I might be wrong: https://answers.unity.com/questions/37115/applicationloadlevel-problem.html

Comment
Add comment · Share
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I restart games with more than one level 1 Answer

How do I make Game Over text stay enabled for a few seconds before the Application Load function runs? 1 Answer

Simple Restart not working 0 Answers

Creating a Restart Button 3 Answers

scene won't reset after restart 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