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 /
  • Help Room /
avatar image
0
Question by Golauszko · Nov 05, 2016 at 09:13 AM · scripting problemlevelrestart

Can't restart a level

I made my first minigame where you shoot to objects and if there are more then 10 objects on the screen you lose. I wanted do let player press R after lose to restart a level but it's not happaning. I see that level is reloading but everything is the same. There is still "Gameover text" display, there are more spawned objects then before and so on. I've tried with enter code here Application.LoadLevel("Minigra");and with SceneManager.LoadScene ("OtherSceneName", LoadSceneMode.Additive);. And it's all the same. Can you help me? There is my code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class Controller : MonoBehaviour {
     public GUIText scoreText;
     public int score;
     public GUIText restartText;
     public GUIText gameOverText;
     public GUIText countTargetText;
 
     public GameObject target;
     private float nextDrop = 0f;
     private float dropInterval = 2f;
     private float changeInterval = 5f;
 
     private bool gameOver;
        
     GameObject[] countTarget;

     void Start()
     {
         score = 0;
         UpdateScore();
         gameOver = false;
         UpdateScore();
         restartText.text = "";
         gameOverText.text = "";
         countTargetText.text = "0/10";
         InvokeRepeating("Target", 1, nextDrop);
 
 
     }
 
     void Target()
     {
         float x = Random.Range(-26f, 26f);
         float z = Random.Range(-22f, 27f);
         Instantiate(target, new Vector3(x, 0, z), Quaternion.identity);
     }
 
     void Update()
     {
         if (Time.time >= nextDrop) //If ready to spawn
         {
             Target();
             nextDrop += dropInterval; //Set next spawn time
             if (Time.time >= changeInterval) //If ready to change spawn interval
             {
                 if (dropInterval > 0.5f) //Change spawn interval to 3/4ths what it was
                     dropInterval *= 0.80f;
                 else //Make sure dropInterval stays above 1.
                     dropInterval = 0.5f;
             }
         }
 
         countTarget = GameObject.FindGameObjectsWithTag("Target");
         countTargetText.text = countTarget.Length + "/10";
 
         
             
         
 
         if (countTarget.Length >= 10)
         {
             gameOverText.text = "Game Over!";
             gameOver = true;
         }
         if (gameOver)
         {
             restartText.text = "Naciśnij R żeby zrestartować";
             if (Input.GetKeyDown(KeyCode.R))
             {
                 SceneManager.LoadScene("Minigra");
                 Destroy(GameObject.FindWithTag("Target"));
                 gameOver = false;
             }
         }
     }
 
     public void AddScore(int newScoreValue)
     {
         score += newScoreValue;
         UpdateScore();
     }
 
 
     void UpdateScore()
     {
         scoreText.text = "Punkty: " + score;
     }
 
 }


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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Zynek · Nov 05, 2016 at 09:30 AM

You could try

 SceneManager.LoadScene ("OtherSceneName", LoadSceneMode.Single);

Also i would advise to reorganize as the code after "LoadScene" should not execute.

 Destroy(GameObject.FindWithTag("Target"));
 gameOver = false;
 SceneManager.LoadScene ("OtherSceneName", LoadSceneMode.Single);

Keep in mind that objects tagged with DontDestroyOnLoad(Object) wont get destroyed https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

Comment
Add comment · Show 2 · 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
avatar image Golauszko · Nov 05, 2016 at 11:27 AM 0
Share

I tried Scene$$anonymous$$anager.LoadScene ("OtherSceneName", LoadScene$$anonymous$$ode.Single); and t's still the same. Before I press R my scena looks like this: http://oi63.tinypic.com/20jo57d.jpg And after http://oi64.tinypic.com/fc91n7.jpg

And my code goes like this:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Scene$$anonymous$$anagement;
 
 public class Controller : $$anonymous$$onoBehaviour {
     public GUIText scoreText;
     public int score;
     public GUIText restartText;
     public GUIText gameOverText;
     public GUIText countTargetText;
 
     public GameObject target;
     private float nextDrop = 0f;
     private float dropInterval = 2f;
     private float changeInterval = 5f;
 
     private bool gameOver;
     
 
     GameObject[] countTarget;
 
     void Start()
     {
         score = 0;
         UpdateScore();
         gameOver = false;
         UpdateScore();
         restartText.text = "";
         gameOverText.text = "";
         countTargetText.text = "0/10";
         InvokeRepeating("Target", 1, nextDrop);
 
 
     }
 
     void Target()
     {
         float x = Random.Range(-26f, 26f);
         float z = Random.Range(-22f, 27f);
         Instantiate(target, new Vector3(x, 0, z), Quaternion.identity);
     }
 
     void Update()
     {
         if (Time.time >= nextDrop) //If ready to spawn
         {
             Target();
             nextDrop += dropInterval; //Set next spawn time
             if (Time.time >= changeInterval) //If ready to change spawn interval
             {
                 if (dropInterval > 0.5f) //Change spawn interval to 3/4ths what it was
                     dropInterval *= 0.80f;
                 else //$$anonymous$$ake sure dropInterval stays above 1.
                     dropInterval = 0.5f;
             }
         }
 
         countTarget = GameObject.FindGameObjectsWithTag("Target");
         countTargetText.text = countTarget.Length + "/10";
 
         
             
         
 
         if (countTarget.Length >= 10)
         {
             gameOverText.text = "Game Over!";
             gameOver = true;
         }
         if (gameOver)
         {
             restartText.text = "Naciśnij R żeby zrestartować";
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R))
             {
                 Scene$$anonymous$$anager.LoadScene("$$anonymous$$inigra", LoadScene$$anonymous$$ode.Single);
 
             }
         }
     }
 
     public void AddScore(int newScoreValue)
     {
         score += newScoreValue;
         UpdateScore();
     }
 
 
     void UpdateScore()
     {
         scoreText.text = "Punkty: " + score;
     }
 
 }


Any idea how to fix this? I stuck with this problem for several hours now I I'm out of idea.

avatar image Golauszko · Nov 06, 2016 at 05:16 PM 0
Share

I spend two days trying diffrent things and still nothing works. It's very strange. It look like after reloding only player position is restarted nothing else. I also don't understand why CancelInvoke(); does not work. Also after restarting displayed colors are difrent as you can see on screen shots Before and After. This is small game so I even tried to make new project and make it again (with the same code) and still nothing. I'm really desperate, I'm afraid to start any bigger procjets without solving this problem.

avatar image
0

Answer by Golauszko · Nov 05, 2016 at 01:08 PM

I think somethng serious is wrong with this code but I still don't know what. I add ' if (countTarget.Length >= 10) { gameOverText.text = "Game Over!"; gameOver = true; CancelInvoke(); '

And text "Game over" is displayed properly but Invoike is still going. Also whan I want to restar it's not working.

Any clue what I'm doing wrong?

Comment
Add comment · Show 1 · 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
avatar image Zynek · Nov 05, 2016 at 11:03 PM 0
Share

I honestly don´t, have you tried to play around with Scene$$anonymous$$anager ? Like UnloadScene for example, then load it again ?

https://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene$$anonymous$$anager.html

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

92 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

Related Questions

Main Menu Help 2 Answers

Rerunning a script on a game object 1 Answer

Restart current level after deathscreen 3 Answers

Level Unlock script help 1 Answer

Best way to Start/Stop script during runtime. 0 Answers


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