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 savanluffy · Aug 20, 2017 at 07:05 PM · scene-loadingscene-switchingdontdestroyonloadenableobject reference

Canvas will only appear on the game screen when its child of a new loaded scene canvas

So I have a gameobject which loads new scenes when nessesarry. The script attached to the game object has a variable which holds the reference to a canvas element(the canvas element has a progressbar). The problem is that after I load a new scene and I want to load a new scene in that loaded scene I lose the reference to the canvas object because I can't set him to DontDestroyOnLoad.

What I have done is I put the canvas element (see commented line in start function()) as a child of the gameobject which doesnt get destroyed.. After I have done that i dont get the warning that I have lost the object reference BUT it wont enable the canvas in the setNewNameAndLoadScene() function.

EDIT: During the runtime i have put my canvas in the new loaded scene canvas it has shown the cav. but the problem is it only works if its in the scene canvas. It does not help if I enable it. So the problem is if I make my cav a child of the new scene canvas. I lose again my reference as it is not longer attached to my gameobject.

Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class SceneLoaderGameObject : MonoBehaviour {
 
 
     private SceneLoader sl;
     public Canvas cav; //loses reference when loading new scene
 
     void Start () {
         sl = new SceneLoader ();
         cav.GetComponent<Canvas> ().enabled = false;
         DontDestroyOnLoad (this.gameObject);
         DontDestroyOnLoad (this);
         //DontDestroyOnLoad (this.gameObject.transform.GetChild(0).GetComponent<Canvas>()); 
     }
 
     public void setNewNameAndLoadScene(string name){
 
         if(name.Length ==0)
             throw new UnityException("Mate length 0");
 
         if (cav != null) {
             Slider slid = cav.transform.GetChild (1).GetComponent<Slider> ();
             Text tx = cav.transform.GetChild (2).GetComponent<Text> ();
             Button bttn = cav.transform.GetChild (3).GetComponent<Button> ();
 
             sl.setNewScene (name);
             sl.setSliderAndTextToChange (slid, tx);
             bttn.onClick.AddListener (() => activateNewScene ());
             cav.GetComponent<Canvas> ().enabled = true;
             StartCoroutine (sl.LoadAsynchron (name, bttn));
 
         }
 
     }
 
     public void  activateNewScene(){
         sl.AsgetOP().allowSceneActivation=true;
 
     }
 }
 
Comment
Add comment · Show 10
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 christoph_r · Aug 20, 2017 at 10:50 PM 0
Share

because I can't set him to DontDestroyOnLoad.

Why not?

avatar image savanluffy christoph_r · Aug 21, 2017 at 12:25 AM 0
Share

Because it was a child of another canvas appearently. I have fixed that as I have put my canvas a child of my gameobject.

avatar image unidad2pete · Aug 20, 2017 at 11:42 PM 0
Share
 DontDestroyOnLoad(cav.transform);

If Canvas have parent:

 DontDestroyOnLoad(cav.transform.parent);

;)

avatar image savanluffy unidad2pete · Aug 21, 2017 at 12:16 AM 0
Share

unidad2pete Well I get the same result as before. It couldnt keep reference because it was a child . But the result is the same. It won't show my canvas on the game screen.

During the run time I have put the canvas which was not destroyed in the scene canvas of the loaded scene and it showed my canvas. When I don't put my canvas (cav) in the new loaded scene canvas it won't show it on the game screen.

avatar image unidad2pete savanluffy · Aug 21, 2017 at 12:28 AM 0
Share

Im not sure why occurs this, put if I put my canvas child of any object, on start Play, canvas turns disabled, but I add,

 cav.enabled = true; // next line of DontDestroyOnLoad(cav.transform.parent);

then, I have no problem, and canvas and her reference on script are showing on scene.

Show more comments

1 Reply

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

Answer by unidad2pete · Aug 21, 2017 at 10:39 AM

Finaly we found the problem:

Canvas is showing but behind of other canvas, fixed when change value of Sort Order of the Canvas to 1 ( Inspector).

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

70 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

Related Questions

How to keep the same gameobject with different transform values in different scenes 1 Answer

Multiple EvenetSystems in Scene - only have 1 after searching though 1 Answer

Don't destroy On Load make object not interactable in new scene 0 Answers

GUI Elements Carry Over To Next Scene 1 Answer

countdown to persist through scenes 2 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