所以我有控制脚本,该脚本可以更改精灵的颜色。当我单击精灵时,它会掉落并停止更改颜色。碰撞时,它会检查颜色是否相同,然后将精灵向上传送回去。所以现在我的问题是:如何使其再次改变颜色?
我的换色器:
public class control : MonoBehaviour
{
public static bool m_isRunning = false;
public static bool loop = true;
public SpriteRenderer m_spriteRenderer;
private Rigidbody2D rb;
public static int random;
public static float loopdelay = 0f;
public static float intervalplayer = 1f; //interval
public static bool changecolorborder = true;
public static bool fall = false;
public static int gravity = 0;
public static bool transform1 = false;
private void Update()
{
random = Random.Range(1, 4);
if (fall)
{
rb.gravityScale = gravity;
}
if (transform1)
{
transform.position = transform.position + new Vector3(0.002f, 1.998f, 0);
}
if(Input.touchCount == 1)
{
Debug.Log("Helloo");
}
}
private void Start()
{
m_spriteRenderer = this.GetComponent<SpriteRenderer>();
rb = GetComponent<Rigidbody2D>();
StartCoroutine(Changecolor(0f));
}
private IEnumerator Changecolor(float loopdelay)
{
while (loop)
{
yield return new WaitForSeconds(intervalplayer);
if (loop)
{
if (random == 1)
{
Color newColor = new Color(0.68f, 0.63f, 0.76f);
m_spriteRenderer.color = newColor;
}
else if (random == 2)
{
Color newColor = new Color(0.89f, 0.25f, 0.23f);
m_spriteRenderer.color = newColor;
}
else if (random == 3)
{
Color newColor = new Color(0.68f, 0.88f, 0.33f);
m_spriteRenderer.color = newColor;
}
else
{
Color newColor = new Color(0.38f, 0.21f, 0.72f);
m_spriteRenderer.color = newColor;
}
}
}
}
private void OnMouseDown()
{
fall = true;
loop = !loop;
gravity = 1;
changecolorborder = !changecolorborder;
}
}
我的碰撞检查器:
public class collision : MonoBehaviour
{
public static int counter = 0;
public static float waitime = 1f;
public Rigidbody2D rb;
public void OnCollisionEnter2D(Collision2D collision)
{
Text textMy = GameObject.Find("Canvas/Text").GetComponent<Text>();
if (collision.gameObject.GetComponent<SpriteRenderer>().color != gameObject.GetComponent<SpriteRenderer>().color)
{
Destroy(collision.gameObject);
}
else
{
counter++;
textMy.text = counter.ToString();
control.gravity = 0;
StartCoroutine(waitfor());
}
}
IEnumerator waitfor()
{
yield return new WaitForSeconds(1);
control.transform1 = true;
yield return new WaitForSeconds(0.033f);
control.transform1 = false;
}
}
答案 0 :(得分:0)
我想像在开始时一样重置您的值
fall = false;
loop = true;
gravity = 0;
changecolorborder = !changecolorborder;
然后再次调用协程
StartCoroutine(Changecolor(0f));