- Home /
Changing alpha value of a canvas from a different game object
Hi all, I am working on a game built from one scene. this scene includes a canvas(called StartMenu) that is basically "covering" the game scene. when the user puts his finger on the right spot, the canvas disappears(using StartMenu.SetActive (false)) and the game starts. I am trying to make the transition "smooth" by adding a fading effect. I attached the canvas with a script from the following tutorial:
I am having problems activating the method FadeMe() for this specific Canvas. this is how iv'e been trying to access it:
If(GameStarted)
{
GameObject go = GameObject.Find("StartCanvas");
fadeScript = go.GetComponent<Fade>();
fadeScript.FadeMe();
}
what happens is that, when the user puts his finger to start the game, you can see the score started increasing but the StartMenu canvas is not fading.
What am I missing here?
Edited(31.5): Following is the code attached on the Canvas game object
using UnityEngine;
using System.Collections;
public class Fade : MonoBehaviour {
public void FadeMe(){
StartCoroutine (doFade ());
}
IEnumerator doFade(){
CanvasGroup canvasGroup = GetComponent<CanvasGroup>();
while(canvasGroup.alpha > 0){
canvasGroup.alpha -= Time.deltaTime / 2;
yield return null;
}
canvasGroup.interactable = false;
yield return null;
}
}
Answer by karma0413 · May 31, 2015 at 12:27 PM
Did you make FadeMe() the function inside the Fade Sript public?
public void FadeMe() {}
Additionally, you did not post any code from within the Fade Script, so we don't know if the problem is how you are accessing the function.. or how you are changing the alpha...
Hi $$anonymous$$arma, thanks for trying to help. i have edited the post with the Fade class
I am not an expert by any means and frankly unfamiliar with co-routines...
However, could you just do me two favors on this script.
*Insert on line 11 inside the doFade "print ("Im trying to fade, but cant");
*insert inside the while-loop (Line13: "print ("$$anonymous$$y alpha is above zero");
Let's see what is not running , and narrow down which line of code is not providing the desired output.
also insert in the DoFade, like on line 12 after you gather the canvasgroup.... Print (CanvasGroup.name);
Sorry, if I don't have a direct answer for you. It looks like it should be working to me also.
Your answer
Follow this Question
Related Questions
Please help my head is burning from this problem : i have multiple gameobject , same script 1 Answer
Script on multiple objects not working properly! 1 Answer
Refresh panel with prefab contained value from json that created using array 0 Answers
cannot drag script to player.Guitext error,cannot drag player script to the player in hierarchy 2 Answers
Is there a way to dynamically attach a script to a GameObject during runtime? 1 Answer