I’ve got a very peculiar situation where my button images are disappearing in certain situations. At the start of the Scene, I am creating a line of buttons based on an array, and these buttons load in Image Resources for their sprites. I also have an effect, whereby a set of curtains on a canvas drop and rise, obscuring what’s behind them. The Curtain Object is parented to another Curtain Object, which is itself parented to a Canvas. The Buttons are parented to a Canvas (to structure the button with other images), which is itself parented to an overall Canvas. The objects do not share a Canvas.
I’ve tracked down the exact line of code it occurs on. It’s line 3 in the following method:
GameObject curtainsCanvas = transform.Find("CurtainsCanvas").gameObject;
curtainFallObject = Instantiate(curtainFallPrefab, curtainsCanvas.transform);
The really strange thing, is that if I pause the game in the editor, the images reappear, and then of course disappear once play is resumed.
Are there any ideas about what could be causing this?
To be honest, I don’t know your exact issue but I will point out a few things!
You should avoid using “Find”, it is bad practice and can often cause problems later on if you make changes. You’re best to set up a reference to that gameObject.
I’m not sure why you have multiple Canvas’ for this? Are your curtains in world-space or are they UI elements? If they are world-space, then ok. If they are UI elements, then you will only need one Canvas which will make it less messy and easier to keep track of.
These two suggestions may or may not fix your problem. But it should at least make it a bit easier to work with. The next thing I can say is, how did you narrow it down to that third line of code? What has indicated that, is there an error?
Tbh, if all your doing is a “curtain fall” I think it would be much easier to make a really simple animation clip that animates the curtain dropping down. Then you can put a “Canvas Group” component on the parent object, you can access this via code to disable/enable it (including making it invisible and visible) as and when you need it. Then reset it and replay the animation when you need it next.