Hello!
I am spawning stars using Instantiate(), and every star has a script which makes it blink (just the alpha value over time), but I want every star to start blinking at the time I spawn it from a random alpha value (so all stars will look like they are blinking/sparking differently). Now, when I spawn a star, it resets every other star to the starting alpha value. As a result, every time I click my mouse on the screen, all stars are being reset and all stars are blinking simultaneously, like the script is being run again from scratch, discarding the previous alpha value.
The script that is spawning the stars :
void Update () {
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
RaycastHit hit;
if( Physics.Raycast( ray, out hit, 100 ) )
{
if( hit.transform.gameObject.name == "Sky" )
{
Vector3 poz = Camera.main.ScreenToWorldPoint(Input.mousePosition);
poz.z = -3;
Instantiate(Star, poz, Quaternion.identity);
StarCount++;
if(StarCount >= 7)
Debug.Log("Win");
}
//Debug.Log( hit.transform.gameObject.name );
}
}
}
The script that is making the stars blink :
public class StarSparkle : MonoBehaviour {
public Color CurrentColor;
public bool Direction = true;
private IRagePixel ragePixel;
public float Delta; //(Mathf.Round(Random.value*10 + 0.1f));
// Use this for initialization
void Start () {
ragePixel = GetComponent<RagePixelSprite>();
CurrentColor.r = 1f;
CurrentColor.g = 1f;
CurrentColor.b = 1f;
CurrentColor.a = Random.value;
}
// Update is called once per frame
void Update () {
if((CurrentColor.a < .4f) && !Direction)
Direction = true;
if((CurrentColor.a > 1f) && Direction)
Direction = false;
Delta = Time.deltaTime/2;
if(Direction)
CurrentColor.a+=Delta;
else
CurrentColor.a-=Delta;
ragePixel.SetTintColor(CurrentColor);
//Debug.Log(CurrentColor.a);
}
}