- Home /
Script stopped working, prefabs behaving strangely
I wrote the following script to make pickups flee from the player while the player is too close to them:
using UnityEngine;
using System.Collections;
public class RunAway : MonoBehaviour
{
public GameObject Player;
public float speed = 1;
private float minDistance = 5f;
private float range;
void Update()
{
range = Vector2.Distance(GameObject.FindGameObjectWithTag("PickUp").transform.position, GameObject.FindGameObjectWithTag("Player").transform.position);
if (range < minDistance)
{
GameObject.FindGameObjectWithTag("PickUp").transform.position = Vector2.MoveTowards(GameObject.FindGameObjectWithTag("PickUp").transform.position, Player.transform.position, -1 * 1 * Time.deltaTime);
}
if (range > minDistance)
{
speed = 0;
}
}
}
It was working perfectly. I began working in some other scripts and now - out of the blue - that script won't work anymore. Only 1 of the pickups (all spawned from a prefab) will run when the player gets close, and always in a diagonal line to the upper right instead of altering his path to escape from the player.
This is extremely frustrating to me, as this was my first ever script that worked and i have zero clue how to get it back. What should i try? And how is it possible that several objects spawned by dragging from the same prefab are behaving in different ways? Different objects have the exact same script, but only some of them work, and even then, incorrectly!!
let me know if you need more details, like the project file or something.
Your answer
![](https://koobas.hobune.stream/wayback/20220612174750im_/https://answers.unity.com/themes/thub/images/avi.jpg)