HI and thanks for reading this guys.
Im at the end of me tether a bit so was wondering if i could pose this question has I cant seem to fit the task into my code.
Problem:
I want to fade out a winning GUI.Box(); object after a set amount of time.
I have placed the action on this task in to an IEnumerator, which i though would be best after looking around and trying out different things that either worked but turned everything off or just didnt produce the result.
I have posted my code below , but to explain it a bit further…
It triggers a gui box event after a bool value is checked from reaching a set figure of items to collect.
What i need help with
could someone please just guide me on how to remove the winning text box after a bit of time correctly, whils also keeping the other wintext box in mind as that will need to show and do the same fade when that is achieved too.
AS A NOTE, on my behalf:
There are two (effectively the same functions in there for two different texts - i know this is not optimizing my code but i am in a bit of a hurry just to get the functionality asap really, cos i need to write it up as working).
Thanks again for reading and feel free to shout stupid comments at my stupidity for not seeing something probably really obvious…
PS: I do read the docs, but perhaps I don`t understand them very well or am just a bit stupid… I hope not, in the end!.
Thanks Gruffy
using UnityEngine;
using System.Collections;
/*ItemController
* Gruffy 2013 - uses static accessibles to obtain score - this can thene be passed around but is dangerous
* ...it should really be placed into a delegate state system i think -future dev point
* THIS IS A FUTURE DEVELOPMENT
*
* The class itself is designed to allow object activations to occur on gameobjects dependent on a clause being met
* In this case, the script has been personlaised to deliver areas and bones current state updates.
* */
public class ItemController2 : MonoBehaviour
{
//collection values
public static int itemCount = 0;
public static int areaCount = 0;
public int totalNumberToCollect = 9;
public int totalAreasToVisit = 9;
//completed bool
public bool storyComplete = false;
//for teleporter pads
public GameObject go = null;
public GameObject finalGo = null;
public GUIStyle style = null;
//strings decs
private string winText1 = " ";
private string winText2 = " ";
//color cache
//alpha for game object
private Color alpha;
//alphas start value
private float alphaVal = 1.0f;
//screen counter-offsets
private Rect lhsRect;
private Rect rhsRect;
//for the style alpha value
private Color color;
void Start()
{
lhsRect = new Rect(Screen.width - Screen.width, Screen.height - Screen.height, 201, 57);
rhsRect = new Rect(Screen.width - 190, Screen.height - Screen.height, 201, 57);
//styleAlpha = style.normal.textColor;
if(go != null)
{
//cache an aplha value as colour
alpha = new Color(1.0f, 1.0f, 1.0f, 0.0f);
go.SetActive(false);
return;
}
if(finalGo != null)
{
//cache an aplha value as colour
alpha = new Color(1.0f, 1.0f, 1.0f, 0.0f);
go.SetActive(false);
return;
}
else
{
Debug.Log("NOT all GameObject have a reference added to them in the ItemController script
Do u need to add some gameobjects in your inspector ");
}
}
void Update()
{
//conditionals
//if not equal to specified total
if(itemCount != totalNumberToCollect)
{
//set child go to false
go.SetActive(false);
}
else
{
//set child go to false
go.SetActive(true);
Color itemAlpha = new Color(0.5f, 0.5f, 0.5f, 0.5f);
go.renderer.material.color = itemAlpha;
}
//if not equal to specified total
if(areaCount != totalAreasToVisit)
{
//set child go to false
finalGo.SetActive (false);
}
else
{
//set child go to false
finalGo.SetActive (true);
Color areaAlpha = new Color(1.0f, 1.0f, 1.0f, 1.0f);
go.renderer.material.color = areaAlpha;
}
}
void OnGUI()
{
lhsRect = new Rect(Screen.width - Screen.width, Screen.height - Screen.height, 201, 57);
rhsRect = new Rect(Screen.width - 190, Screen.height - Screen.height, 201, 57);
//string to display in GUI box
string itemText = "Total Bones " + itemCount;
string areaText = "Total Areas Visited " + areaCount;
string itemsToGetText = "Bones To collect " + (totalNumberToCollect - itemCount);
string areasToGetText = "Areas To collect " + (totalAreasToVisit - areaCount);
//make GUI box to hold item counts
//GUI.Box (new Rect(Screen.width - (Screen.width /2), 20, 130, 20), itemText);
if(itemCount <= totalNumberToCollect)
{
//style.font.material.color = Color.white;
if(itemCount >= totalNumberToCollect)
{
winText1 = "Well Done
You Collected all " + itemCount + " bones!!!" + "
Return them to Kitty Jay`s Grave…
If you are ready…
Look around you for a portal to her resting place
&return her bones!!!";
StartCoroutine(Fade (0.005f));
GUI.color = color;
if(color.a > 0.001f)
GUI.Box (new Rect(Screen.width/2 - 250, (Screen.height/3), 500, 175), winText1, style);
//co routine
}
else
{
GUI.BeginGroup(lhsRect,style);
//position far left hand side
GUI.Box (new Rect(0,15, 190, 20), itemsToGetText, style);
//GUI.Box (new Rect(Screen.width - (Screen.width), 20, 170, 20), itemsToGetText, style);
GUI.Box (new Rect(0, 30, 190, 20), itemText, style);
GUI.EndGroup();
}
}
//this conditional checks against the visited areas , as each is colllected the count increments and is used for nested conditional inside
if(areaCount <= totalAreasToVisit)
{
//if total reached
if(areaCount >= totalAreasToVisit)
{
//assign text
winText2 = "Well Done
Youve seen all " + areaCount + " areas" + " Remember what was said at the tree... If you feel you
ve seen everything, go back to finish off
your experience";
GUI.Box(new Rect(Screen.width/2 - 175, (Screen.height/3), 350, 80), winText2, style);
// start co routine
StartCoroutine(Fade (0.005f));
}
else
{
GUI.BeginGroup(rhsRect, style);
//position far right hand side new Rect(0, 30, 190, 20)
//GUI.Box (new Rect(Screen.width -170, 20, 170, 20), areasToGetText, style);
//GUI.Box (new Rect(Screen.width -180, 60, 180, 20), areaText, style);
GUI.Label (new Rect(0, 15, 190, 20), areasToGetText, style);
GUI.Label (new Rect(0, 30, 190, 20), areaText, style);
GUI.EndGroup ();
}
}
}
IEnumerator Fade(float fadeAmount)
{
while(true)
{
color.a -= fadeAmount;
if(color.a <= 0.001f)
{
print ("Done!");
return false;
}
yield return new WaitForEndOfFrame();
}
}
}