- Home /
Question by
Seldkam · Nov 24, 2018 at 05:46 PM ·
instantiatetransformparentparent-childparent transform
Silly Instantiation Issue
I'm pretty sure there is a tiny detail in here somewhere causing my prefab to spawn very far away from the spawner. I've tried both using "true" and "false" parameters for the parent / child Boolean, and it hasn't helped much, it just makes the spawning location bizarre, and more bizarre :|
Here's the code: (updated because constantly calling GetComponent is bad)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class spawnEnemy : MonoBehaviour {
[SerializeField]
private GameObject enemyToConfigure;
[SerializeField]
private GameObject playerTarget;
[SerializeField]
private Transform playerTransform;
[SerializeField]
private Transform spawnAtSpawnerTransform;
private GameObject enemyToSpawn;
private float countDownToSpawn;
private Transform facePlayerToChange;
private GameObject playerTargetToChange;
// Use this for initialization
void Start ()
{
countDownToSpawn = 10f;
facePlayerToChange = enemyToConfigure.GetComponent<facePlayer>().playerFacePlayer = playerTransform;
playerTargetToChange = enemyToConfigure.GetComponent<enemyController>().playerEnemyController = playerTarget;
enemyToSpawn = enemyToConfigure;
}
// Update is called once per frame
void Update ()
{
countDownToSpawn -= Time.deltaTime;
Debug.Log(countDownToSpawn);
if (countDownToSpawn < 0)
{
InstantiateEnemy(facePlayerToChange, playerTargetToChange, enemyToSpawn);
countDownToSpawn = 10f;
}
}
void InstantiateEnemy(Transform transform, GameObject playerToFace, GameObject toSpawn)
{
enemyToSpawn = Instantiate(toSpawn, spawnAtSpawnerTransform, false);
facePlayerToChange = transform;
playerTargetToChange = playerToFace;
}
}
Comment
Hmm well if you look at the "Instantiate Enemy" method itself I simply deleted the spawnatspawnertransform, and the false, and it works a little better now. Before it was spawning in the sky, now it at least spawns on the ground-- but not where I want it.