0
votes

i've seen this question asked online a bunch but the solution was never typed out in script, which is what i'm trying to figure out. I'm instantiating an object every 4 seconds and that works fine. but the prefab i'm instantiating is bigger in the game. What's weird is that in the scene view (while the game is playing) the prefab is the correct size. What is also odd is that the enlarged version is that of the original sprite scale. But I have other prefabs with edited scales that instantiate from plenty of other scripts correctly. So what I'm trying to do is to tell the script to instantiate this prefab at the local scale, because I read that it may be a world scale issue. My question is, how do I type that out???? I tried going, Instantiate (enemy, transform.position * transform.localScale.x, transform.rotation * transform.localScale.y); but that didn't work out obviously :/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemySpawnerBasic : MonoBehaviour {

public GameObject enemy;
public Transform enemys;
public float spawnTimer = 0f;
public float spawnDelay = 4f;
private bool spawned;

void Update () 
{
    SpawnEnemy ();
    spawnTimer -= Time.deltaTime;
}

void SpawnEnemy()
{
    if (!spawned && spawnTimer <= 0) {

        Instantiate (enemy, transform.position, transform.rotation);
        spawnTimer = spawnDelay;

    }
}
}
1
is the scale on the prefab correct? in the sprite import settings, what's the pixels per unit? - Ido Ben Shalom
pixels per unit is 100. the scale on the prefab is .5 on x and y scale. The prefab is correct. It's resized to .5 rather than the default 1 x 1 scale - Joe Clark
maybe the parent of the instantiated enemies is twice the scale? a few related screenshots might help resolve the issue - Ido Ben Shalom
there is no parent. ah screw it. I'll just increase the pixels per unit on this one enemy. He's the only one being instantiated by this object which the scaling is becoming an issue with anyway. thanks i forgot about the pixels per unit route. - Joe Clark
hope i could help more! also you should consider spawning the enemies using unity's InvokeRepeating method instead of the Update method docs.unity3d.com/ScriptReference/… - Ido Ben Shalom

1 Answers

0
votes

instantiateInWorldSpace

Pass true when assigning a parent Object to maintain the world position of the Object, instead of setting its position relative to the new parent. Pass false to set the Object's position relative to its new parent.

The docs don't make it clear, but this same boolean applies to rotation and scale as well as position. I believe you will need to pass false to get the behavior you want.

This parameter (and a similar one on SetParent(...)) was added with the new UI due to the way that RectTransforms behave and are expected to behave. DMGregory had an answer about this over on GameDev SE.