- Home /
Simple fade script unexpectedly fading all UI elements,Simple Fade script unexpectedly fading all UI elements
hey guys, I have this script I made (pulled off the internet) to fade my announcement box when it is active. When it is done fading, I wanted to then deactivate it, and reset the alpha channel. When I testing the following code, when the announcement box was set active, all objects in my UI canvas ALL fade out... I wouldn't assume this is expected behavior, but I am a noobie...
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Announcements : MonoBehaviour
{
public Text announcementText;
private Color alphaColor;
private float timeToFade = 3.0f;
// Start is called before the first frame update
void Start()
{
alphaColor = this.gameObject.GetComponent<Image>().material.color;
alphaColor.a = 0;
}
// Update is called once per frame
void Update()
{
if (this.gameObject.activeInHierarchy == true)
{
this.gameObject.GetComponent<Image>().material.color = Color.Lerp(this.gameObject.GetComponent<Image>().material.color, alphaColor, timeToFade * Time.deltaTime);
}
//something about can select more than one enemy with this skill, maybe even quips from bosses
}
}
why is this code fading out all of my UI elements on the canvas!! what is the solution to this problem? My announcement box is not the only thing with the image component, but it is the only object with this script attached, and even my silders are all fading away. i also tried to replace this.gameobject with gameobject.find("announcementbox") but that didnt fix it either. :(
Answer by mryuri666 · Nov 24, 2021 at 12:33 PM
try to instantiate a public GameObject Announcement ; and then use Announcement.GetComponent instead os this.Gameobject.GetComponent (Dont forget to assign gameobject in inspector)
Thanks for the quick reply @mryuri666 before bed last night I tried yoinking some scripts I didn't understand from the interwebs, to no avail. I still have the exact script in the original question, and will be modifying it per your instructions.
1. After creating public GameObject Announcement, 2. dragging the AnnouncementBox into the new slot in the script in the inspector (script which is on AnnouncementBox itself) 3. replacing all of this.gameObject with new Announcement variable 4. Results in no change in behavior, as far as i can tell, all objects in hierarchy fade out, even ones not children of this exact UICanvas
I am lost, but touching component is confusing me. After everything fades away, and I check on the faded image's Image component (Including my AnnouncementBox with script attached), the color pop up window says that the alpha channel is 255.
FURTHERMORE after I exit playmode, those game objects dont reappear! They actually stay faded, and I have to close and reopen Unity to see them again!
Your answer
Follow this Question
Related Questions
Can't see EmptyObject in scene viewer,Can't See Empty Object in Scene View 3 Answers
Code not working right for puzzle game 2 Answers
iTween CameraFade not working in some cases (related to camera's cullingMask) 3 Answers
Wait and fading script 1 Answer
Fade texture on collision/trigger with certain tag or layer? 2 Answers