Convert Javascript to c#

how can i convert this to C#

#pragma strict

var spawnPoints : Transform[];  // Array of spawn points to be used.
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var amountEnemies = 20;  // Total number of enemies to spawn.
var yieldTimeMin = 3;
var yieldTimeMax = 5;  // Don't exceed this amount of time between spawning enemies randomly.
var i : int;
public var theFsm : PlayMakerFSM;

function Spawnblock() 
{ 
    for (i=0; i<amountEnemies; i++)
    {
      yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));  // How long to wait before another enemy is instantiated.
 
      var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
      var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];  // Randomize the spawnPoints to instantiate enemy at next.
 
      Instantiate(obj, pos.position, pos.rotation); 
}}

You should attempt to learn to do this yourself, as it really isn't hard. A simple Google search would provide plenty of examples. There is even [this][1] utility someone created to help, although it can't do it all, you'll still have to fill in some gaps. [1]: http://files.m2h.nl//js_to_c.php

1 Answer

1

using UnityEngine;
using System.Collections;

public class SomeScript : MonoBehaviour
{
 
  public Transform[] spawnPoints;
  public GameObject[] enemyPrefabs;
  public int amountEnemies = 20;  // Total number of enemies to spawn.
  public int yieldTimeMin = 3;
  public int  yieldTimeMax = 5;  // Don't exceed this amount of time between spawning enemies randomly.
  public int i;
 
  public IEnumerator Spawnblock() 
  { 
      for (i=0; i<amountEnemies; i++)
      {
        yield return new WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));  // How long to wait before another enemy is instantiated.
 
        var obj = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)]; // Randomize the different enemies to instantiate.
        var pos = spawnPoints[Random.Range(0, spawnPoints.Length)];  // Randomize the spawnPoints to instantiate enemy at next.
 
        Instantiate(obj, pos.position, pos.rotation); 
      }
  }
 
}

thanks but i get this error: Assets/codemines.cs(15,15): error CS1624: The body of codemines.Spawnblock()' cannot be an iterator block because void' is not an iterator interface type

Just make SpawnBlock return an IEnumerator instead of void.

can you show me how to do this? sorry

Updated the code above

And here is the answer to your next question :), you need to use StartCoroutine(Spawnblock());