Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 OctoMan · Jun 06, 2015 at 09:24 AM · uibuttontextcanvaspersistence

Canvas Button persistence

Hi all,

i have buttons and other UI elements. I have onClick event to Call Load() Save() Delete() funktions. Now if i load another Scene abd go back to the first Scene the connection to the gameobject with the funtions inside are gone. How could i get the connections back to the buttons?

Same to text elements. I tried to find the gameobject in the awake() but after reloading the scene the connection again is missing.

The script for text example.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class MoneyScript : MonoBehaviour {
 
     public static MoneyScript moneyS;
 
     public int coins = 0;
     public Text coinText;
     private Text CT;
 
     void Awake()//happends before start
     {
         //make sure the gameobject is a prefab
         if(moneyS == null)//if moneyS does not exist 
         {
         DontDestroyOnLoad (gameObject);//make a gameobject persistent
         moneyS = this;//make moneyS this script
         }
         else if(moneyS != this)//if it is not this
         {
             Destroy(gameObject);//destroy it
         }
     }
 
     void Start()
     {
         // I want to get the textfield here .... 
         CT = GameObject.Find ("coinText").GetComponent<Text> ();
         if(CT != null)
         coinText = CT;
     }
 
     public void addCoins(int money)
     {
         coins += money;
         coinText.text = coins.ToString ();
     }
 }

Comment
Add comment · Show 4
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 OctoMan · Jun 06, 2015 at 09:40 AM 0
Share

I found out, that canvas buttons shouldn't have a reference to persistent gameobjects, then they work.

But how about text? I need that to show the player how much money they have.

avatar image Mouton · Jun 06, 2015 at 09:41 AM 1
Share

Because of "DontDestroyOnLoad", the method Start won't be called when loading a new scene. Did you tried "OnLevelWasLoaded" method ?

 void OnLevelWasLoaded()
 {
          CT = GameObject.Find ("coinText").GetComponent<Text> ();
          if(CT != null)
          coinText = CT;
 }

You should really avoid GameObject.Find, prefer to use GameObject.FindWithTag

avatar image Narv · Jun 06, 2015 at 10:45 AM 0
Share

Side question... why do you have coinText as public (and set in the inspector) but find it using a private variable CT and then overwriting what was put in using coinText. But you never check if coinText was set in the inspector before just overwriting it.

If you do set it in the inspector and just want to make sure it is set properly, you can check the value of coinText and then that will save the cost of Find() if it's not needed.

avatar image OctoMan · Jun 06, 2015 at 11:11 AM 0
Share

Thanks alot, works like a charm. I didn't knew there is such a function.

Narv: I tried to fix my problem, so i tried to find the gameobject and get it back to it's initial state. Basicly i could make it private and search the object in Start() and OnLevelWasLoaded().

But since i have a persistent gameobject i could't just get it back because of $$anonymous$$outon's answer.

0 Replies

· Add your reply
  • Sort: 

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

22 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

Related Questions

Missing canvas elements on Build & Run. 2 Answers

UI 4.6 canvas button, how to make it invisible on gameplay and how to make it visible when player is dead? 3 Answers

5.3.5 - Blurry UI text after switching canvasses - until text is refreshed by turning visibility off/on or changing resolution 4 Answers

Unity UI Text Blurriness 0 Answers

Multiple Text of canvas with One script ?? 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