- Home /
Instantiate creates multiple clones.
I want to instantiate an enemy (navmeshagent) when i click "q" and it works. The only problem is that everytime i want to add one again it seems to clone the clones as well. So at first in creates only one, the second time it creates two, the third one four, the fourth time eight and so on. I'm new to programming. Does anyone know how to fix this? This is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class npcspawn1 : MonoBehaviour {
public GameObject s1;
GameObject s1Clone;
public Transform SP1;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown("q"))
{
s1Clone = Instantiate(s1, SP1.position, SP1.rotation) as GameObject;
}
}
}
Answer by SirPaddow · Jun 08, 2019 at 06:41 PM
Does s1 contain the "npcspawn1" monobehaviour? If it does, that means that the monobehaviour is duplicated on all your clones, and the keydown is managed in each, which could explain this weird behaviour.
Answer by itaielidan · Jun 08, 2019 at 09:39 PM
That means that mabye the enemy has that scrpit attached to it and then it runs when It instantiates
Your answer
![](https://koobas.hobune.stream/wayback/20220612202836im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
extra GameObjects Spawning 2 Answers
Insantiate object to parent without changing scale? 0 Answers
Instantiating infinite platforms in a row problem 1 Answer
Gap Between Scrolling Background Objects 0 Answers
max amount of enemy present? 2 Answers