- Home /
Question answered, answer accepted.
UI elements don't appear anymore after launching the game
Hello, I don't usually ask for help directly and always find the answer to my problem online but this time I'm clearly stuck ! I think my problem is similar to this one which is a year old and has had no answer since. The difficulty lies in the fact I don't know if it's a bug or a misunderstanding of myself.
When I first launch the project, the UI appears as it should. But after launching the game a first time, all the non-sprite elements have disappeared.
What my scripts do during play mode involves some gameObject.SetActive(true/false);
and from what I understand even if I leave some objects disabled, they should still appear in Editor Mode as they are re-enabled automatically.
I tried enabling/disabling game objects with no success however when I changed one of the disappeared game objects layer from 'UI' to 'Default' and started/stopped the game, everything reappeared. I'm not able to understand why it behaves this way.
If you have any idea or if you have already encountered this problem, please let me know how I should solve it. If you need more information, just let me know I'll add it to my question ! Thanks.
EDIT : I'm clearly wondering if this could be a script related issue. Does enabling and disabling UI elements while in Play Mode change the way UI elements appear in Scene Mode ?
EDIT2 : I feel so dumb, after playing a bit with it, trying to add some Canvas.ForceUpdateCanvases ();
everywhere, I found the case in which I had this problem : I have a blinking text and when I start the game while it is 'off', it just produces the above bug. However, if I start the game while the blinking text is 'on' it just works fine thus explaining the erratic nature of the issue I encountered. I'll just take a break and then will look at the blinking code (5 lines long :| ) to see what was the cause of the issue. Even if I had put some code here, I wouldn't have thought it was caused by such a meaningless part of all the code I currently have. Sorry for the inconvenience, I'll keep you up !
EDIT 3: Found the problem and put it as an answer below. Also removed the picture as they aren't relevant to the issue. Thanks for the help though.
I checked canvas tag and it is set to 'Untagged' whether my UI elements have already disappeared or not :
However, this is the kind of things I wouldn't have been aware of. I'm not confident with layers and such.
Answer by DotGames7 · Jul 04, 2017 at 12:29 PM
That was it ! Here is the faulty code :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Blink : MonoBehaviour {
Text text; float speed = 1f;
Color col;
void Start (){
text = gameObject.GetComponent<Text> ();
col = text.material.color;
}
void Update () {
col.a = Mathf.Round(Mathf.PingPong(Time.time * speed, 1.0f));
text.material.color = col;
}
}
Here you can see I was changing the default material of the 'text' component which was the default material of all my non-sprite components to an alpha of 0. When I exited play mode, the new color was keeped by the material.
I should have added another material to the blinking text or chose another way of making it blinking like enabling and disabling it.
I feel like answering your own question is a bit strange but I'll do it in order to help anyone who has the same problem given the fact this issue can be a real pain. It was really dumb !