Hey guys,
So what I want to do is once a user collects an item a number will appear in the middle of the screen then fade away.
Green text - When an item is picked up.
Red text - When an item is dropped.
What I have so far:
public Text bookweightA;
public Text bookweightS;
public Text mapweightA;
public Text mapweightS;
Color visibleG = new Color(124f, 255f, 130f, 100f);
Color visibleR = new Color(255f, 124f, 124f, 100f);
void OnTriggerStay(Collider other)
{
...
bookweightA.color = visibleG;
Now the problem I’m having is that the text is not changing colour, it just appears white, however the alpha values works!
I believe color values are from 0.0 to 1.0
I tried something different instead and got the desired color, how would I fade it back out?
This is my attempt thus far?
Color c = bookweightA.color;
c.a = 100;
bookweightA.color = c;
timeleft -= Time.deltaTime;
if (timeleft == 0)
{
c.a = 0;
bookweightA.color = c;
}
This is the output of my debug.log of the timeleft variable everytime I pick up and drop the object.
Anybody know why?
I declared timeleft at the top of the script:
float timeleft = 2.0f;
So I picked up and dropped my object up roughly 200 times and as the timer become equal to or less than 0 my text disappeared as desired, so there is a problem with my timer pausing? Anyone know how I can fix it?
EDIT: Is utilising a timer the best way about going this, is there a way I can do something like…
Wait 2 seconds
Change alpha of the text back to 0.?
DuffyT
October 24, 2015, 8:43am
6
Hotpots:
So I picked up and dropped my object up roughly 200 times and as the timer become equal to or less than 0 my text disappeared as desired, so there is a problem with my timer pausing? Anyone know how I can fix it?
EDIT: Is utilising a timer the best way about going this, is there a way I can do something like…
Wait 2 seconds
Change alpha of the text back to 0.?
I suggest you take a look at coroutines. :')
Hotpots
October 24, 2015, 10:26am
7
How would I go about doing?
Is it best to utilise co-routines for this problem or just a simple timer?
Have you tried using CrossFadeColor() ?
Develop once, publish everywhere! Unity is the ultimate tool for video game development, architectural visualizations, and interactive media installations - publish to the web, Windows, OS X, Wii, Xbox 360, and iPhone with many more platforms to...
Thanks Fajl, this has helped me get a step further towards my solution.
I used CrossFadeAlpha() instead, it works perfectly as I wanted the first time I pick up the item.
The text component appears “+1.2kg” then fades after 2secs, however if I drop the item and try pick it up again it does not work
if (other.gameObject.tag == "book" && Input.GetKey("e"))
{
itemweight = 1.2f;
ScoreManager.currentweight += itemweight;
other.gameObject.SetActive(false);
bookimage.color = visible;
isholding = true;
Color c = bookweightA.color;
c.a = 100;
bookweightA.color = c;
bookweightA.CrossFadeAlpha(0, 2, false);
Full script (if needed):
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ObjectPickup : MonoBehaviour
{
public Image bookimage;
public Image mapimage;
public Text bookweightA;
public Text bookweightS;
public Text mapweightA;
public Text mapweightS;
public static float itemweight;
bool isholding;
//float timeleft = 2;
Color visible = new Color(0f, 0f, 0f, 100f);
Color invisible = new Color(0f, 0f, 0f, 0f);
GameObject book;
GameObject map;
void Start()
{
book = GameObject.FindGameObjectWithTag("book");
map = GameObject.FindGameObjectWithTag("map");
//anim = GetComponent<Animator>();
}
void Update()
{
if (Input.GetKey("q") && isholding == true)
{
DropItem();
}
}
void OnTriggerStay(Collider other)
{
// If the entering collider is a book...
if (other.gameObject.tag == "book" && Input.GetKey("e"))
{
itemweight = 1.2f;
ScoreManager.currentweight += itemweight;
other.gameObject.SetActive(false);
bookimage.color = visible;
isholding = true;
Color c = bookweightA.color;
c.a = 100;
bookweightA.color = c;
bookweightA.CrossFadeAlpha(0, 2, false);
/*timeleft -= Time.deltaTime;
Debug.Log(timeleft);
if (timeleft <= 0)
{
c.a = 0;
bookweightA.color = c;
timeleft = 0;
}*/
}
// If the entering collider is a map...
else if (other.gameObject.tag == "map" && Input.GetKey("e"))
{
itemweight = .2f;
ScoreManager.currentweight += itemweight;
other.gameObject.SetActive(false);
mapimage.color = visible;
isholding = true;
Color c = mapweightA.color;
c.a = 100;
mapweightA.color = c;
}
else
{
}
}
void DropItem()
{
if (bookimage.color == visible)
{
itemweight = 1.2f;
ScoreManager.currentweight -= itemweight;
bookimage.color = invisible;
Vector3 position = GameObject.Find("CollectableSpawnPoint").transform.position;
GameObject item = Instantiate(book, position, Quaternion.identity) as GameObject;
item.gameObject.tag = "book";
item.gameObject.SetActive(true);
}
if (mapimage.color == visible)
{
itemweight = .2f;
ScoreManager.currentweight -= itemweight;
mapimage.color = invisible;
Vector3 position = GameObject.Find("CollectableSpawnPoint").transform.position;
GameObject item = Instantiate(map, position, Quaternion.identity) as GameObject;
item.gameObject.tag = "map";
item.gameObject.SetActive(true);
}
isholding = false;
}
}
Hotpots
October 24, 2015, 8:48pm
10
I’m still unsure why CrossFadeAlpha() isn’t working after dropping the item once