Question by
Creative Inventory · Sep 10, 2015 at 06:34 PM ·
c#javascripttexture2dconvert
Convert a java script in a c#??? thank you
Can someone help me convert this javascript spawner into a c# spawner script 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.<Renderer>().bounds.size.x/2;
var x2 = transform.position.x + GetComponent.<Renderer>().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
Answer by Positive7 · Sep 10, 2015 at 09:48 PM
MyScript.cs
using UnityEngine;
public class MyScript : MonoBehaviour {
public GameObject enemy;
// Variable to know how fast we should create new enemies
public float spawnTime = 2;
void Start() {
// Call the 'addEnemy' function every 'spawnTime' seconds
InvokeRepeating ("addEnemy", spawnTime, spawnTime);
}
// New function to spawn an enemy
void addEnemy() {
// Variables to store the X position of the spawn object
// See image below
var x1 = transform.position.x - GetComponent<Renderer> ().bounds.size.x / 2;
var x2 = transform.position.x + GetComponent<Renderer> ().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);
}
}
Thank you, it works perfectly. Also do you know if you can add a wave to my spawn script, where it has a certain timer to each spawn and a speed, where it can be accessible in the inspector, that would be much appreciated. Thank you. @Positive7