- Home /
Duplicate a Prefab
Hello, so i've made a pretty simple but good game. But today when i tried to make some enemies i duplicated a enemy wich was my "Zombie". The original "Zombie" worked with the animations and when he came up to me he hit me, but the copied version just walked up to me and didn't hurt me. So what sould i do?
The script for the chasing is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChaseZombie : MonoBehaviour
{
public Transform player;
static Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle(direction, this.transform.forward);
if (Vector3.Distance(player.position, this.transform.position) < 45 && angle < 180)
{
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isAttacking", false);
if (direction.magnitude > 1)
{
this.transform.Translate(0, 0, 0.05f);
anim.SetBool("iswalking", true);
anim.SetBool("isAttacking", false);
}
else
{
anim.SetBool("iswalknig", false);
anim.SetBool("isAttacking", true);
}
}
else
{
anim.SetBool("iswalking", true);
anim.SetBool("isAttacking", false);
}
}
}
Double-check in the Inpsector of the Prefab. Are all the objects etc dragged into all the public fields you created? I know I had similar problems where I'd saved a gameobject as a prefab, the one in the Hierarchy that I first created worked fine, but the prefab had some items missing, because they were impossible to drag into it at "prefab level" (for want of better ter$$anonymous$$ology lol) . I had to then find a way of adding the objects via code ( ins$$anonymous$$d of using public (and then draggin in inpsector).
$$anonymous$$ethods such as GameObject.FindObjectWithTag() helped me here.
I hope this helps
It's probably the damaging script that has an issue, not this one..
Yes defeintely agreed. I hadnt read properly that is working other than not hurting the player
would the fact the "iswalking" is spelt wrong on line 34 cause the script to not compile?
Answer by danteswap · Dec 05, 2017 at 05:25 PM
Delete All Zombies From hierarchy Than Add the Working one In Scene And test if Animations Are Working, if Working then Select The Zombie From Scene duplicate it by ctrl+c , ctrl+v, And test if both Are Working Or Not. if Working Then Delete duplicated one And Create The prefab From 1st Zombie By dragging it into The Assets folder When Done Delete the zombie In the Scene then drag Created prefab from Assets folder to Scene And Duplicate it Like 10 Or 20 Time as per your Need .
Also OP remember, if you make a change to the prefabbed object from within the hieracrchy, you need to click 'Apply' on the prefab for it to apply to every instance of the prefab from then on, otherwise you're only affecting the one instance and not the prefab itself
Ok, i deleted them all but only the first one i pasted worked. Thanks for all help everyone thought!
Answer by $$anonymous$$ · Dec 05, 2017 at 05:39 PM
So i draged the prefab into the assets folder but again only the first one worked, so what should or can i test now?
If first One Worked All Duplicates Should work Same Unless The Reference On Duplicates Are To the First One .It doesn't Sound Good But need some shot Or Video For understanding whats going on.
It's fixed now so thank you all guys! Every little step made one big! Credits to @danteswap @megabrobro @shadyproductions .
yeah long story short, i copied the prefab and it went wrong but later i made a new script pasted the old script in and it's done! So remember you can't copy scripts. Thanks!
Your answer
Follow this Question
Related Questions
Instantiated coconuts not working 0 Answers
Using Instantiate on a Rigidbody rather than a whole GameObject 1 Answer
Creating Prefab causes scripts to break, Copy/Paste Gameobject doesn't 0 Answers
Instatiate prefab at another prefab issue 1 Answer
Prefab Variables Changing in the Original, Not the Copies 2 Answers