- Home /
how to disable function that have InvokeRepeating
Hi, sorry if this question is duplicate, but I did my best to find the salutation by my self but I didn't found a solution to solve my problem.
I have code that insatiate object by InvokeRepeating, and what I want is, if the object touch collider2d the instantiation have to stop, and this is what I did.
Script B (Control)
void OnTriggerEnter2D (Collider2D collider2d)
{
if (collider2d.tag == "Checkpoint")
{
point++;
scoreText.text = point + "";
Debug.Log ("CheckPoint");
}
if (collider2d.tag == "GameOver")
{
Debug.Log ("touched");
Destroy (player);
scoreText.text = "0";
spawnCircle sc = GetComponent<spawnCircle>();
sc.stopGame();
}
}
Script A (spawnCircle)
public static bool theGameisActive = true;
void Start () {
if (theGameisActive == true)
{
Vector3 middle = new Vector3 (0.0f, 0.0f, 0.0f);
int objectIndex = Random.Range (0, circleSpawning.Length);
Instantiate (circleSpawning [objectIndex], middle, Quaternion.Euler (new Vector3 (0, 0, Random.Range (180, 90))));
InvokeRepeating ("circleSpawn", delayTimer, delayTimer);
Debug.Log ("The game is just started");
}
}
void circleSpawn ()
{
if (theGameisActive == true)
{
Vector3 middle = new Vector3 (0.0f, 0.0f, 0.0f);
int objectIndex = Random.Range (0, circleSpawning.Length);
Instantiate (circleSpawning [objectIndex], middle, Quaternion.Euler (new Vector3 (0, 0, Random.Range (0, 360))));
}
}
public void stopGame ()
{
theGameisActive = false;
Debug.Log ("stopGame");
}
When I did this code I get an error: NullReferenceException: Object reference not set to an instance of an object Control.OnTriggerEnter2D (UnityEngine.Collider2D collider2d) (at Assets/scripts/Control.cs:56)
Can I find someone help me to solve this problem please
Thanks,
You need to tell us what line 56 of Control.cs is, because that's where the error is. Are these scripts attached to the same object? You stop invoking a method with CancelInvoke(), but I don't see you call that anywhere.
sc.stopGame(); This is line 56, and I just tried CancleInvoke(), but also not working with me
Your answer
![](https://koobas.hobune.stream/wayback/20220612084512im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
store instantiated in Array. c# 1 Answer
Problem when setting variable in an instantiated gameobject 0 Answers
instatiate gameobject after 1 second 1 Answer
How to tell the number of instantiated object 2 Answers
Instantiate a GameObjet C# 2 Answers