How to make clone of gameobject have the same properties as the original gameobject


I have this script that i have on a empty gameobject, what it does is it makes the enemy always face the player. So that when the x position of the player is greater than the x position of the enemy, the enemysprite mirrors.

EnemyFlip script:

using UnityEngine;
using System.Collections;

public class EnemyFlip : MonoBehaviour {

	private GameObject enemies;
	private GameObject player;
	private float scale;

	void Start () {

		player = GameObject.FindGameObjectWithTag ("Player");
		enemies = GameObject.FindGameObjectWithTag ("Swordsman");
		scale = enemies.transform.localScale.x;

	void Update () {

		if (enemies.transform.position.x < player.transform.position.x)
			enemies.transform.localScale = new Vector3(-scale, scale, 1);
			enemies.transform.localScale = new Vector3(scale, scale, 1);


When i make a clone of the enemy gameobject (spawning) using this script;

using UnityEngine;
using System.Collections;

public class SwordsmanSpawn1 : MonoBehaviour {
	public GameObject Swordsman;
	public Vector3 spawnSpot;
	void Start() {

		Instantiate(Swordsman, spawnSpot, transform.rotation);

… the clone of the enemy inherets the movement script of the original enemy, but not the “EnemyFlip” script.

Only the original Enemy will mirror its sprite, but both of them is moving towards the player.

How do i make it so that the clone also uses the EnemyFlip script?

Thank you in advance :slight_smile:


Make a prefab of the square. Then use code to make the prefab appear in the place u want it.

Your problem is that you have many Swordsman objects, but you’re only ever getting the first one, assigning just that single object to ‘enemies’.

Each of your cloned objects is then telling that first object which direction to face.

Instead of storing enemies and referencing enemies.transform, reference this.transform.