Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 KasunL · Dec 26, 2015 at 11:18 AM · instantiateprefabimagereloadc #

Bug after reloading the scene

The mobile game i'm currently creating displays the score after Game over, and reloads the level. Behind the text i'm displaying an background image prefab. The game works fine until i spawn the image.

Code:

 // set gameOver flag to true.
     public void GameOver()
     {
         GameoverText.text = "Game Over!";
         gameOver = true;
     }
 
     ......
     ......
     // Instantiate GameOverBGAsset1 -- the image prefab, and GameOverCanvas -- a reference to 'Canvas2', And make GameOverBGAsset1 a child of Canvas2.
     if (gameOver)
     {
                 GameObject GameOverImg2 = Instantiate(GameOverBGAsset1, GameOverBGPos.transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
                 GameObject GameOverCanvas = GameObject.FindGameObjectWithTag("Canvas2");
                 GameOverBGAsset1.transform.SetParent(GameOverCanvas.transform);
 
                 restartText.text = "Tap to Replay";
                 scoreTextLarge.text = "Score: " + score;
                 restart = true;
 
                 break;
             } 
 
    ....
    ....
    // restart when screen is touched
    if (restart)
    {
                tap = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 5));            
                if ((tap.x != 0) || (tap.y != 0) || (tap.z != 0))
                {
                  Application.LoadLevel("Application.LoadedLevel");
                }
     }


When i reload the level it displays "Game Over!" -- coz it's displayed before i spawn the image -- and get stuck there. Is it actually a problem with the spawned prefab, or something else?

Thanks for any input.

P.S. This ONLY happens when i reload the level. In the first run, all the intended text and BG image is displayed as expected.

Comment
Add comment · Show 2
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 corn · Dec 27, 2015 at 03:25 AM 0
Share

For starters, I assume Application.LoadLevel("Application.LoadedLevel"); should actually be Application.LoadLevel(Application.loadedLevel);

avatar image KasunL corn · Dec 27, 2015 at 03:12 PM 0
Share

I'm sorry, that's a typo. In the code, there's no quotes. Thanks

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by PabloUnityArgentina · Dec 27, 2015 at 11:03 AM

The GameoverText.text is a static property. It belongs to the class, not to the object. So no matter how many times you reload the scene. It remains the same, because the statics members doesn't belong to any scene. In order to resolve this, you have to restart your static property when you reload the scene.

Comment
Add comment · Show 4 · 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 KasunL · Dec 27, 2015 at 02:47 PM 0
Share

But my problem is it's not displaying the BG image in the second run. Is is something do with Gameover Text??

avatar image PabloUnityArgentina KasunL · Dec 27, 2015 at 03:17 PM 0
Share

You are setting the parent of a prefab in the line 15, here:

 GameOverBGAsset1.transform.SetParent(GameOverCanvas.transform);

"GameOverBGAsset1" is a prefab, not a GameObject .It is not instantiated in a scene, so that line of code is wrong. You have to change it in orden to set the parent of the real GameObject, instantiated in the scene, not the prefab:

 GameOverImg2.transform.SetParent(GameOverCanvas.transform);


avatar image AibNihan · Feb 17, 2018 at 10:33 PM 0
Share

Thanks $$anonymous$$en You Save $$anonymous$$y day. i don't know that static property needs to restart. Thank you

avatar image Oleksii_Stetsenko · Jan 09 at 02:06 PM 0
Share

U saved me =)

p.s. For those who don't know where to reset your static variables - you can reset them right in your LoadScene method. Like this:

 public void RestartGame()
     {
         SceneManager.LoadScene("SampleScene");
         GameControllScript.dinamiteCratePicked = false;
         GameControllScript.netCratePicked = false;
         GameControllScript.netCount = 0;
         GameControllScript.dinamiteCount = 0;
     }

where GameControllScript is the name of script with your static variables.

avatar image
0

Answer by Wilson28300 · Dec 27, 2015 at 09:22 AM

I think it should be Application.LoadLevel(Application.LoadedLevel); without double quotes. Otherwise it will check for a scene named "Application.LoadedLevel".

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 KasunL · Dec 27, 2015 at 02:49 PM 0
Share

I'm sorry, that's a typo. In the code, there's no quotes. Thanks

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

8 People are following this question.

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

Related Questions

Instantiate Prefab (Image) and show it on the screen? 1 Answer

Instantiate a prefab just one time 1 Answer

Problem with using Vector3.MoveTowards on randomly instantiated target 1 Answer

assign different values to prefabs on instantiation 2 Answers

How to associate underlying data structure to prefab at runtime? 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