Enemy Spawner with different size enemies

2D C#

I am creating a Vampire Survivors like game. So I have an enemy spawner that spawns enemies at random locations around the player. It is working great for that part. The next step for the spawner is to make it spawn “boss” enemies. The only thing I want to change visually for the bosses is their size.

I thought this would be as easy creating a new prefab with the same sprite and scaling it up but, the enemies still come out at the normal scale. The spawner is not a child of any other object and I’ve tried increasing the scale of the spawner thinking that might be what is controlling the scale of the enemy with no luck. The code is currently set up to spawn the BossObject every time for testing puposes.

I am hoping someone could provide some insight as to why a prefab thats scaled to 3,3,3 is coming out as 1,1,1 when instantiated through code.

Thanks!

Spawner code

public class EnemySpawner : MonoBehaviour
{
	[SerializeField]
	private GameObject enemy;
	[SerializeField]
	private GameObject BossObject;
	
	private GameObject newEnemy;
	private GameObject BossEnemy;
	private SpriteRenderer rend;
	private int randomSpawnZone;
	private float randomXposition, randomYposition;
	private Vector3 spawnPostion;
	public GameObject playerToFollow;
	public float SpawnDelay;
	public float SpawnRate;
	
	public float MinRange = 0f;
	public float MaxRange = 10f;
	public float BossSpawnRate = 5f;
    // Start is called before the first frame update
    void Start()
	{
		playerToFollow = GameObject.FindGameObjectWithTag("Player");
		InvokeRepeating("SpawnNewEnemy", SpawnDelay, SpawnRate);
    }

    // Update is called once per frame
    void Update()
    {
	    transform.position = playerToFollow.transform.position;
    }

	private void SpawnNewEnemy()
	{
		randomSpawnZone = Random.Range(0, 4);

		switch (randomSpawnZone)
		{
			case 0:
				randomXposition = Random.Range(transform.position.x + -11f, transform.position.x + -10f);
				randomYposition = Random.Range(transform.position.y + -8f, transform.position.y + -8f);
				break;
			case 1:
				randomXposition = Random.Range(transform.position.x + -10f, transform.position.x + 10f);
				randomYposition = Random.Range(transform.position.y + -7f, transform.position.y + -8f);
				break;
			case 2:
				randomXposition = Random.Range(transform.position.x + 10f, transform.position.x + 11f);
				randomYposition = Random.Range(transform.position.y + -8f, transform.position.y + 8f);
				break;
			case 3:
				randomXposition = Random.Range(transform.position.x + -10f, transform.position.x + 10f);
				randomYposition = Random.Range(transform.position.y + 7f, transform.position.y + 8f);
				break;
		}

		spawnPostion = new Vector3(randomXposition, randomYposition, 0f);
		newEnemy = Instantiate(BossObject, spawnPostion, Quaternion.identity);

	}
}

Spawner GameObject
![Spawner|690x294](upload://tuypY1AbvFSArJEoMBWiwiWuanx.jpeg)


BossObject assigned to spawner script
![BossPrefab|417x500](upload://ezDrzjQReGZjLCS5QMdMY4GLtxL.jpeg)





Have you tried scaling your newEnemy GameObject to a value you want it to have after instantiating?

newEnemy = Instantiate(BossObject, spawnPosition, Quaternion.identity);
newEnemy.transform.localScale = new Vector3(3, 3, 3);
2 Likes

Thank you for the reply!

No luck with that at first but it led me to do other testing. I found that even if I manually change the scale of an already spawned enemy, it reverts back to the original scale. So I knew I had to be changing the scale SOMEWHERE in a script. Turns out the enemy movement script was using the scale to flip it to face the player and it was hardcoded to 1,1,1 facepalm.

So the solution was to change it from this:

    void Update()
	{		
		targetPosition = playerToFollow.transform.position;
		
		if((transform.position - targetPosition).magnitude > stopDistance)
     {
			transform.position += moveSpeed * (targetPosition - transform.position).normalized * Time.deltaTime;
		}
		if (oldPostion < transform.position.x)
	    {
			transform.localScale = new Vector3(-1,1,1);
	    }
		else if (oldPostion > transform.position.x)
	    {
			transform.localScale = new Vector3(1,1,1);
	    }
	    
		oldPostion = transform.position.x;
	}

To this:

    void Update()
	{		
		targetPosition = playerToFollow.transform.position;
		float tmpX = 0f;
		if ((transform.position - targetPosition).magnitude > stopDistance)
		{
			transform.position += moveSpeed * (targetPosition - transform.position).normalized * Time.deltaTime;
		}
		if (oldPostion < transform.position.x)
		{
			//Negative is Right
			tmpX = transform.localScale.x < 0 ? transform.localScale.x : transform.localScale.x * -1f;
			transform.localScale = new Vector3(tmpX, transform.localScale.y , transform.localScale.z);
	    }
		else if (oldPostion > transform.position.x)
		{
			//Positive is Left
			tmpX = transform.localScale.x > 0 ? transform.localScale.x : transform.localScale.x * -1f;
			transform.localScale = new Vector3(tmpX, transform.localScale.y , transform.localScale.z);
	    }
	    
		oldPostion = transform.position.x;
	}

Your suggested line of code is what I will use to scale them now that I’ve found the root cause. That is much cleaner way of doing it than creating a second prefab, serializing a second field etc.

1 Like