- Home /
Question by
unity_DLT1KPGCDxuDZg · Oct 02, 2019 at 05:02 PM ·
c#unity 5codepageinvoke
cancle invokeReapeating not working
I was trying to cancle the invoke from running the whole time, because the sprite color should only change every 3 scenes. Please help me :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class color : MonoBehaviour
{
public float speedchange = 1f;
SpriteRenderer m_SpriteRenderer;
void Start()
{
InvokeRepeating("ChangeColor", 1f, speedchange);
}
void ChangeColor()
{
int random = Random.Range(1, 3);
if (random == 1)
{
m_SpriteRenderer = GetComponent<SpriteRenderer>();
m_SpriteRenderer.color = Color.blue;
}
else if(random == 2)
{
m_SpriteRenderer = GetComponent<SpriteRenderer>();
m_SpriteRenderer.color = Color.red;
}
else
{
m_SpriteRenderer = GetComponent<SpriteRenderer>();
m_SpriteRenderer.color = Color.green;
}
}
private void OnMouseDown()
{
CancelInvoke("ChangeColor");
}
}
Comment
You should check if it is being called. Try putting Debug.Log("Cancelling Invoke"); or something in the On$$anonymous$$ouseDown method.
Also, are you aware that On$$anonymous$$ouseDown is only called if the mouse button is pressed when the cursor is over the gameobject and that object would require a collider?
Your answer
Follow this Question
Related Questions
My EnemyAI is flying towards me 1 Answer
Unity UFO tutorial question about code 0 Answers
Roll a Ball Tutorial: Ball floats in the air when I enter play mode. 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers