- Home /
How can I prevent a prefab script from affecting all instances when entering a collider?
Hello to everyone!
I have a script attached to an enemy prefab that makes it start moving when the player enters it's trigger collider. My problem is that once the player enters an instance's trigger collider, all of the other instances start moving as well instead of just the instance I want.
This is the script attached to the prefab:
void FixedUpdate() {
if (activated)
rb.velocity = (Vector3.down * 1.5f * enemySpeed);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player") )
{
activated = true;
}
I will appreciate any help or tips.
Thank you!
There's probably something wrong with your code. Unity will never move all of the instances. So, can you post the whole script?
Answer by hawksandwichgames · Jun 29, 2018 at 11:53 AM
Hey! Not sure what's causing this. Based on what you said, I'm assuming you think that prefabs somehow share their code. For instance, telling one instance of a prefab to move via the script attached to it might cause another to move. This isn't the case, so the problem has to be elsewhere. Maybe your script is static, in which case it would behave this way (because instead of instancing, they act as one instance.) That's about the only thing I could think of, actually.