Instances of the same prefab behaving differently
I am currently working on a dungeon game, in which the main enemies are skeletons. I have created a prefab with all the colliders, animator, script, etc. on it, but now it is not really working. By that I mean that about 50% of the clones of the prefab work exactly as they are supposed to, but the other 50% don't move. They play the animations and can reduce player health when you're close enough, but they just stay in one place. I've never seen this happen before, and on an earlier version of my game the enemies worked perfectly fine 100% of the time. However, after running into and solving a different problem (one with similar symptoms on the enemies, but in that case due to unneeded location keyframes, which I removed), this started happening. Does anyone know what could be causing this? And (more importantly) how to fix it? Edit: I should note that the ones that do move often move backwards, sideways, or at the wrong speed. I haven't touched the script since the earlier version where it worked fine. Any ideas what the problem might be? Should I not have deleted the location keyframes in Blender? Or does it have to do with something else, like the root bone I set up (I noticed that turning "Apply Root Motion" on or off will affect whether half or none of the skeletons work correctly)? This is the script:
using UnityEngine;
using System.Collections;
public class skeleton1_script : MonoBehaviour {
public float speed;
public float myViewAngle;
public float attackdist;
public int startH;
public float damage;
private int attacktime = 0;
private CharacterController contrlr;
private bool insight;
private Animator anim8;
private GameObject Player;
private int Health;
//private Collider myTrigger;
// Use this for initialization
void Start () {
contrlr = GetComponent<CharacterController> ();
anim8 = GetComponent<Animator> ();
Player = GameObject.FindWithTag ("Player");
Health = startH;
//myTrigger = GetComponent<SphereCollider> ();
//state = anim8.GetCurrentAnimatorStateInfo (1);
}
// Update is called once per frame
void Update () {
if (Health <= 0) {
return;
}
transform.position = new Vector3 (transform.position.x, 0, transform.position.z);
insight = false;
Vector3 target = new Vector3 (Player.transform.position.x, Player.transform.position.y + 1, Player.transform.position.z);
Vector3 direction = target - transform.position;
float angle = Vector3.Angle (direction,transform.forward);
if (angle <= myViewAngle * .5f) {
RaycastHit hit;
if (Physics.Raycast (new Vector3(transform.position.x, transform.position.y + 1, transform.position.z), direction, out hit)) {
if (hit.transform.name == "Player") {
insight = true;
}
}
}
if (insight == true) {
transform.LookAt(new Vector3(target.x, transform.position.y, target.z));
if (Vector3.Distance(transform.position, Player.transform.position) >= attackdist) {
anim8.SetBool("moving", true);
contrlr.SimpleMove(direction*speed);
anim8.SetBool("attacking", false);
}
else {
anim8.SetBool("attacking",true);
//if (state.normalizedTime <= 0.166f) {
//myTrigger.enabled = true;
//}
//else {
//myTrigger.enabled = false;
//}
if (contrlr.velocity == new Vector3(0, 0, 0)) {
anim8.SetBool("moving", false);
}
}
}
else {
anim8.SetBool("moving", false);
anim8.SetBool("attacking", false);
}
}
void Hurt (int amount) {
Health -= amount;
if (Health <= 0) {
contrlr.enabled = false;
StartCoroutine(Die());
}
//print ("" + Health);
}
void OnTriggerStay (Collider other) {
if (insight == true) {
if (other.tag == "Player") {
if (attacktime ==0) {
other.SendMessage ("Hurts", damage);
}
//myTrigger.enabled = false;
attacktime = (attacktime + 1)%20;
}
}
}
IEnumerator Die () {
anim8.SetTrigger("die");
for (int i = 0; i <= 20; i++) {
transform.position -= new Vector3(0, 1.5f*Time.deltaTime, 0);
yield return null;
}
gameObject.SetActive (false);
}
}
I have same problem as you are. If I add 4 prefab 2 prefab working fine. Why is it that. Did you solve your problem Caleb_1701?
No, I haven't solved it. I'm looking into possible solutions, but so far no success.
Wow, still no answer from anyone. I guess this isn't a common problem, so not a lot of people have dealt with it.
I edited the question so the script is now there. I also pointed out some things that may or may not be relevant to solving the problem; I'm hoping they are relevant.
Hey there, I'm having the same problem with my main character prefab. Her public move multiplier and public gravity multiplier are all out of wack when she instantiates, making her fall way too fast and move at half speed. However, when I click on the Instantiated prefab in the scene hierarchy, the problem goes away. This prefab had been working for months and changed pretty suddenly after 5.6. The fact that it "fixes" itself just by clicking on the prefab in hierarchy makes me think this is a bug. Does that happen for you?
Your answer
![](https://koobas.hobune.stream/wayback/20220612064114im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is there a way to control when lighting is generated? 0 Answers
How to safely delete a prefab? 0 Answers
Instantiate prefab on inspector drop 0 Answers
Unity overwrites prefab values in runtime 0 Answers
Instantiate more than 1 prefab 0 Answers