Question by
upariver · Apr 26, 2016 at 06:07 PM ·
c#instantiatevector3arraylist
[C#] Object not spawning where it should?
I have a GameObject which spawns a line of prefabs which forms platforms from the place the GameObject is placed in the level. The issue is that the place where I spawn them is different as to the place where I place them. Can anyone give me any suggestions of what i'm doing wrong?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Spawner : MonoBehaviour {
public int numPlat;
public GameObject[] PlatSpawner;
public GameObject PlatformToSpawn;
public Vector3 spawnPosition;
public float NextSpawnPosition;
public SpriteRenderer spriteChosen;
// Use this for initialization
void Start () {
Debug.Log("Number" + numPlat);
spawnStuff();
}
public void spawnStuff()
{
for (int i = 0; i < numPlat; i++)
{
//NextSpawnPosition++;
float PlatWidth = PlatformToSpawn.GetComponent<SpriteRenderer>().bounds.size.x;
NextSpawnPosition = PlatWidth + NextSpawnPosition;
Debug.Log("" + PlatWidth);
spawnPosition = new Vector3 (this.transform.position.x + NextSpawnPosition, transform.position.y,transform.position.z );
Instantiate(PlatformToSpawn, spawnPosition, Quaternion.identity) ;
}
}
}
Comment