- Home /
Are Tags the best solution for this? Duplicating an item that duplicates itself a set amount of times.
I have some code that causes a gameObject to out so far and duplicate itself multiple times before it destroys itself. I need to limit the amount of times that this happens. It might happen once or it might happen 3 times. The duplicated objects will duplicate as well and then it will go on forever.
I am just trying to figure out the best way to limit this. GetComponent seems like it would not work well. Tags seem to be the easiest way to implement this. When a gameObject with "Dupe3" spawns it will spawn gameObjects with tag "Dupe2" etc. Until its finally done.
Answer by HammockHead21 · Sep 23, 2021 at 05:38 AM
You could expose a public property on the Monobehaviour. Something like below.
public class Duplicator : Monobehavior {
[SerializeField] Duplicator prefab;
public int level;
void Start() {
if (level > 0) {
GameObject g = Instantiate<Duplicator>();
g.GetComponent<Duplicator>().level = level - 1;
}
}
}
Thanks I will try this out and get back to you once I get done.
Your answer
Follow this Question
Related Questions
Delete some unused tags and game objects tag changes unity 5 2 Answers
Destroying Multiple Objects with Tags 1 Answer
instantiate a random object from multiples via 'tag' 1 Answer
nullReferenceException when comparing tag in a search for a specific parent. help. 1 Answer
Making object switch between two tags every 3 seconds 3 Answers