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 fcbharoon · Sep 19, 2017 at 05:00 AM · unity 5gameobjectscene-loadingactivate

Unity - How to activate a canvas or gameobject when a scene restarts?

I have one scene which has many levels when i am playing and i want to restart this level i am loading the scene and activating the gameobject of the this level. But the scene restarts without activating the gameobject of that level. Here is my code

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 public class PlayLevel : MonoBehaviour {
 
     // Use this for initialization
     GameObject Level;
     public GameObject Menus;
     Button button;
     void Start () 
     {
         button = GetComponent<Button> ();
         button.onClick.AddListener(delegate() {
             this.Restart();    
         });
         Level = GameObject.FindGameObjectWithTag ("Levels");
         Debug.Log (Level.name);
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     public void Restart()
     {
         Time.timeScale = 1;
         SceneManager.LoadScene (SceneManager.GetActiveScene().name);
         Debug.Log (Level.name);
         Menus.SetActive (false);
         Level.gameObject.SetActive (true);
     }
 }
 

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 game4444 · Sep 19, 2017 at 05:15 AM

Hi, one thing can you confirm please, Debug.Log in restart is printing levelName or not? What i think you should activate level gameObject in start function. Because when ever you will reLoad scene. It will reset your all values and conditions. One thing you could do make public static bool reStart and set value true when you are pressing restart button. And in start check if (reStart == true) then activate level. and after activating on next line set bool to false. Public static bool keeps its value on reloading scene.

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 fcbharoon · Sep 19, 2017 at 05:17 AM 0
Share

I have a canvas and it has many panels for level messages i gave the Levels tag to all the panels but the debug.log line is giving me an error of null reference.

avatar image
0

Answer by NorthStar79 · Sep 19, 2017 at 05:15 AM

  using UnityEngine;
  using System.Collections;
  using UnityEngine.UI;
  using UnityEngine.SceneManagement;
  public class PlayLevel : MonoBehaviour {
  
      // Use this for initialization
      GameObject Level;
      public GameObject Menus;
      Button button;
      void Start () 
      {
          button = GetComponent<Button> ();
          button.onClick.AddListener(delegate() {
              this.Restart();    
          });
          Level = GameObject.FindGameObjectWithTag ("Levels");
          Debug.Log (Level.name);
 
          Menus.SetActive (false);
          Level.gameObject.SetActive (true);
      }
      
      // Update is called once per frame
      void Update () {
      
      }
      public void Restart()
      {
          Time.timeScale = 1;
          SceneManager.LoadScene (SceneManager.GetActiveScene().name);
          
      }
  }
  

changes you made resets too when you reload a scene, activating that objects in start maybe solve your problem .

Comment
Add comment · Show 5 · 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 fcbharoon · Sep 19, 2017 at 05:22 AM 0
Share

It restarts the scene without activating the level gameobject same as before.

avatar image fcbharoon · Sep 19, 2017 at 05:25 AM 0
Share

The debug.log successfully prints the name of the level but when the scene restarts the level gameobject is not activated nor the menus gameobject deactivates.

avatar image NorthStar79 fcbharoon · Sep 19, 2017 at 05:53 AM 0
Share

any NullRefecance error on console?

avatar image fcbharoon NorthStar79 · Sep 19, 2017 at 06:05 AM 0
Share

Nope but i think its the execution problem the debug line prints very quickly then the scene reloads.

Show more comments

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

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

Apparent Problem with smartfox crashing unity editor 0 Answers

How to call NetworkClient.Ready() ? 1 Answer

Calling an Audio Source on one game object from a script on another game object..? 1 Answer

GameObject keeps acting like gameObject. 1 Answer

Script on multiple objects not working properly! 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