Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SamualMeh · Nov 02, 2014 at 04:27 PM · c#javascriptenemyspawnconverting

JS to C# converting issues..

Hello all, I am creating a level in unity for a uni assignment; I followed this 3part tutorial a few months back: http://www.youtube.com/watch?v=1C9zbTRCWoM and got it working. The problem was when I tried converting it to a C# script so that I could use it with the PhotonNetwork.

It is working for the most part; there are no errors but for some reason the spawn times are off and all enemies after the first spawn faster than the random.range(yieldTimeMin, yieldTimeMax) which is between 1-3 seconds. I will post my JavaScript code and also my C# code(i'm not used to using for loops and yields in C# and was told something about invoke repeating would be a good idea too so if that helps then I'd love to know how I would word it into the script.

Thanks!

 #pragma strict
 
 //This Script is dealing with all of the enemy spawning such as how often, where, wave numbers etc.
 //It is placed onto the object that holds all of the spawnpoints in the game (Holder_EnemySpawnPoints).
 //@HideInInspector to hide the variable on the next line from the Inspector.
 
 
 var enemySpawnPoints : Transform[];
 var enemyPrefabs : GameObject[];
 
 var yieldTimeMin : float = 1;
 var yieldTimeMax : float = 3;
 
 var spawnXOffsetMin : float = 0;
 var spawnXOffsetMax : float = 0;
 var spawnZOffsetMin : float = 0;
 var spawnZOffsetMax : float = 0;
 
 var enemyCounter : int = 0;
 var startingSpawnNumber : int = 4;
 var isSpawning : boolean = false;
 
 var waveNumber : int;
 
 
 function SpawnEnemies(wave : int)
 {
     var spawnNum = (startingSpawnNumber + 3 * (waveNumber - 1));
     isSpawning = true;
     
     for(var i = 0; i < spawnNum; i++)
     {
         yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
         
         var spwnObj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];
         var spwnPos : Transform = enemySpawnPoints[Random.Range(0, enemySpawnPoints.Length)];
         
         Instantiate(spwnObj, spwnPos.position + Vector3(Random.Range(spawnXOffsetMin, spawnXOffsetMax), 0, Random.Range(spawnZOffsetMin, spawnZOffsetMax)), spwnPos.rotation);
         enemyCounter++;
     }
     
     isSpawning = false;
 }
 
 function UpdateWave()
 {
     waveNumber++;
     SpawnEnemies(waveNumber);
 }
 
 
 
 function Start()
 {
     waveNumber = 1;
     SpawnEnemies(waveNumber);
 }
 
 function Update()
 {
     if(enemyCounter == 0 && !isSpawning)
     {
         UpdateWave();
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SamualMeh · Nov 02, 2014 at 12:59 PM 0
Share

Here is the C# version that I have so far:

 using UnityEngine;
 using System.Collections;
 
 public class EnemySpawner : $$anonymous$$onoBehaviour
 {
     public Transform[] enemySpawnPoints;
     public GameObject[] enemyPrefabs;
 
     float yieldTime$$anonymous$$in = 1;
     float yieldTime$$anonymous$$ax = 3;
 
     float spawnXOffset$$anonymous$$in = 0;
     float spawnXOffset$$anonymous$$ax = 0;
     float spawnZOffset$$anonymous$$in = 0;
     float spawnZOffset$$anonymous$$ax = 0;
 
     int enemyCounter = 0;
     int startingSpawnNumber = 4;
     int waveNumber;
     bool isSpawning = false;
 
 
     void Start()
     {
         waveNumber = 1;
         SpawnEnemies (waveNumber);
     }
 
     void Update()
     {
         if(enemyCounter == 0 && !isSpawning)
         {
             UpdateWave();
         }
     }
 
     void SpawnEnemies(int wave)
     {
         int spawnNum = (startingSpawnNumber + 3 * (waveNumber - 1));
         isSpawning = true;
 
         for(int i = 0; i < spawnNum; i++)
         {
 
             StartCoroutine("SpawnTime");
             enemyCounter++;
 
         }
         isSpawning = false;
     }
 
     void UpdateWave()
     {
         waveNumber++;
         SpawnEnemies (waveNumber);
     }
 
     public IEnumerator SpawnTime()
     {
         GameObject spwnObj = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];
         Transform spwnPos = enemySpawnPoints[Random.Range(0, enemySpawnPoints.Length)];
 
         yield return new WaitForSeconds (Random.Range (yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax));
         Instantiate(spwnObj, spwnPos.position + new Vector3(Random.Range(spawnXOffset$$anonymous$$in, spawnXOffset$$anonymous$$ax), 0, Random.Range(spawnZOffset$$anonymous$$in, spawnZOffset$$anonymous$$ax)), spwnPos.rotation);
         enemyCounter++;
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gjf · Nov 02, 2014 at 05:37 PM

congratulations for formatting the code properly. it sometimes seems like a lost art ;)

add some Debug.Log's into it to determine the random time.

you don't need enemyCounter++; in SpawnTime() AND SpawnEnemies()

in SpawnEnemies() you're not using the parameter that you pass...

Start() could just have waveNumber = 0; - the spawning will be done in the first Update() cycle.

InvokeRepeating() could work for regular intervals but since you want random times, it's not relevant here.

and there's nothing to stop the same spawn positions and objects being used in this code. is that intentional?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Help have script know how many enemies are in the world 1 Answer

Destroy and Spawn an Enemy 1 Answer

can anyone please help with enemy wave spawn? 0 Answers

Trying to write enemy spawn in JS. 2 Answers

how can i do a headshot with a rigidbody bullet? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges