- Home /
Question by
ThePhantomGoat · Sep 04, 2018 at 03:06 AM ·
instantiaterepeating
Stopping InvokeRepeating after 5 blocks have been placed.
Hi, Im having a problem limiting how many times that invoke repeating is called, I want it to stop placing cars once it has placed 5 of them, how can I script that? Here is my current script,
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class InfiniteCar : MonoBehaviour {
public GameObject car;
public int carLimit;
private int carCount;
void Start()
{
InvokeRepeating("Inst", 2f, 2f);
}
void Inst()
{
Instantiate (car, transform.position, transform.rotation);
}
private void Update()
{
if (carLimit < carCount)
CancelInvoke();
}
}
Comment
Answer by Juzper · Sep 04, 2018 at 03:28 AM
Change these:
void Inst()
{
Instantiate(car, transform.position, transform.rotation);
carCount = carCount + 1;
}
private void Update()
{
if (carLimit == carCount)
{
CancelInvoke("Inst");
}
}
Also, make sure you set the carLimit.
Also, it would be better to cancel invoke after adding +1, ins$$anonymous$$d of checking it everyframe.
void Inst()
{
Instantiate(car, transform.position, transform.rotation);
carCount = carCount + 1;
if (carLimit <= carCount)
{
CancelInvoke("Inst");
}
}
Your answer
Follow this Question
Related Questions
InvokeRepeating affecting parts of script 1 Answer
Checking if object intersects? 1 Answer
Making image array along two objects 0 Answers
Instantiated Bullet Force not being applied 0 Answers
New GUI and Inventory problem. 1 Answer