Question by
Creative Inventory · Sep 19, 2015 at 12:02 PM ·
spawnnot workingy-axis
Spawn Script on Y axis not working!!!?
Can someone please help me with my spawn script. I have a spawn script attach to a cube placed in my game that spawn prefabs on that cube (stretched out) but on the X axis, when I try to place the same script on a cube on the Y axis , the prefabs just spawn at the same place and not randomly on the cube. Could somebody help me , so that the cube on the Y axis spawns randomly on the cube (stretch out) and not at the same place. Thank you Here's my script:
#pragma strict
// Variable to store the enemy prefab
public var enemy : GameObject;
// Variable to know how fast we should create new enemies
public var spawnTime : float = 2;
function Start() {
// Call the 'addEnemy' function every 'spawnTime' seconds
InvokeRepeating("addEnemy", spawnTime, spawnTime);
} // New function to spawn an enemy
function addEnemy() {
// Variables to store the X position of the spawn object
// See image below
var x1 = transform.position.x - GetComponent.<Collider>().bounds.size.x/2;
var x2 = transform.position.x + GetComponent.<Collider>().bounds.size.x/2;
// Randomly pick a point within the spawn object
var spawnPoint = new Vector2(Random.Range(x1, x2), transform.position.y);
// Create an enemy at the 'spawnPoint' position
Instantiate(enemy, spawnPoint, Quaternion.identity);
}
Comment
use Debug.Log to see which values you're really getting.