Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kilix19980 · Jul 14, 2016 at 10:44 PM · script.

Scripting help

Hello dear coders i need your help i made a code whit the help of youtube and i need help the code works perfectly it spaws enemys and waves waits for me to kill all the enemys then starts a new wave but there is one problem whit the code it onky alows me to use one tipe of prefab (enemy) how can i modify the code to spaw multiple tipes of enemys this is the code

 using UnityEngine;
 using System.Collections;
 
 public class wawespawner : MonoBehaviour 
 {
     public enum SpawState {Spawning, waiting, counting }; 
 
     [System.Serializable]
     public class Wave
     {
         public string name; 
         public Transform enemy;
         public int count;
         public float rate;
     }
 
     public Wave [] Waves;
     private int nextWave = 0;
 
     public Transform[] spawnpoints;
 
     public float TimeBetweenWaves = 5f;
     private float wawecountdown;
 
     private float Searchcountdown = 1f;
 
     private SpawState state = SpawState.counting;
 
     void Start ()
     {
         if (spawnpoints.Length == 0)
         {
             Debug.LogError ("We need SpawPoints");
         }
 
 
         wawecountdown = TimeBetweenWaves;
     }
 
     void Update ()
     {
         if (state == SpawState.waiting) 
         {
             if (!Enemyisalive ()) 
             {
                 WaveComploted ();
             } 
             else
             {
                 return;
             }
         }
         if (wawecountdown <= 0) 
         {
             if (state != SpawState.Spawning)
             {
                 StartCoroutine (SpawWave (Waves [nextWave]));
             }
         }
         else 
         {
             wawecountdown -= Time.deltaTime;    
         }
     }
 
 
     void WaveComploted ()
     {
         Debug.Log ("WaveCompleted");
 
         state = SpawState.counting;
         wawecountdown = TimeBetweenWaves;
         if (nextWave + 1 > Waves.Length - 1) {
             nextWave = 0;
             Debug.Log ("We comploted all waves");
         } 
         else
         {
             nextWave++;
         }
 
 
     }
 
 
     bool Enemyisalive ()
     {
         Searchcountdown -= Time.deltaTime;
         if ( Searchcountdown <= 0f)
         {
             Searchcountdown = 1f;
             if (GameObject.FindGameObjectWithTag ("Enemy") == null)
             {
                 return false;
             }
 
         }
         return true;
     }
 
     IEnumerator SpawWave (Wave wave)
     {
         Debug.Log ("SPawming wawe" + wave.name); 
         state = SpawState.Spawning;
 
         for (int i = 0; i < wave.count; i++) 
         {
             SpawEnemy (wave.enemy);
             yield return new WaitForSeconds (1 / wave.rate);
         }
 
         state = SpawState.waiting;
         yield break; 
     }
     void  SpawEnemy (Transform enemy)
     {
         Debug.Log ("SPawning enemy" + enemy.name);
 
         Transform sp = spawnpoints [Random.Range (0, spawnpoints.Length)];
         Instantiate (enemy, sp.position, sp.rotation);
 
     }
 }
 
 
     
Comment
Add comment · Show 2
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 gjf · Jul 14, 2016 at 08:38 PM 0
Share

from a quick glance at the code, it looks like it should be possible. it's dependent on the type of object that you put in the enemy variable in the Waves array. are you populating that in the inspector?

you'd have to do something else if you wanted multiple types spawned in a single wave...

avatar image kilix19980 gjf · Jul 14, 2016 at 10:11 PM 0
Share

Thank you for your reply could you point me to the direction were i could make such a code that would add more tipes of enemys but whit wave tipen of spawner like this one

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by joshua-lyness · Jul 15, 2016 at 12:31 AM

You want to use an array. An array is like a list of variables. To declare an array, you want to replace

 public Transform enemies;

with :

 public Transform[] enemies;

Now in the inspector, you drag and drop the enemies GameObjects into the enemies variable. You most likely don't want them to be Transform[] anyway, you probably want GameObject[].


Now you want to randomly choose enemies in this array to spawn. To do this, it's fairly simple. Above your line :

 Instantiate (enemy, sp.position, sp.rotation);

you want to write this :

 int randomInt = Random.Range(0, *length of your array of enemies*);

What this does is before it instantiates your enemy, it makes a random number between the range your specify. Since we have an array of enemies, we need to change the instantiate() line to use the randomInt to choose an enemy. Replace the Instantiate() line with this :

 Instantiate (enemy[randomInt], sp.position, sp.rotation);

That will choose from your list of enemies, and instantiate any random one of them. Hope this helped, have a good day!


Just a little tip, make sure you are more specific with your title, make it something like "randomly select GameObjects," since people are more likely to notice it. :)

Comment
Add comment · Show 3 · 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
avatar image kilix19980 · Jul 16, 2016 at 02:06 PM 0
Share

thank you for your reply i changed the public Transform ;

to

 pubic GameObject [] enemy ;

added the lines as you wrote but i got a bunch load of erros and here

 Instantiate (enemy[randomInt], sp.position, sp.rotation);

you missed one of the ")" so i added in the end but still bunch of errors :(((

Assets/Scripts/$$anonymous$$anagers/wawespawner.cs(108,25): error CS1503: Argument #1' cannot convert UnityEngine.GameObject[]' expression to type `UnityEngine.Transform'

avatar image joshua-lyness · Jul 16, 2016 at 03:41 PM 0
Share

If you look at the error, it has (108,25) in it. This means the 25th character on line 108 is the error, nothing to do with the instantiate. Its saying it cant convert GameObject to Transform, which if you look on that line, youre trying to pass a GameObject array into the function. If you follow that line down to where the function is declared, you can see the parameter is of type Transform, yet youre trying to put a GameObject[] in. Change it to GameObject[] enemy and itll work.

avatar image kilix19980 joshua-lyness · Jul 16, 2016 at 04:35 PM 0
Share

well i still get errors ...iam terible at scripting iam beter at 3d modeling but iam tired of just making models i whant to add em to a game and iam sorry to bother you but could you help me out i know iam a pain in the a** but whats wrong whit my code this is the changed version using UnityEngine; using System.Collections;

 public class wawespawner : $$anonymous$$onoBehaviour 
 {
     public enum SpawState {Spawning, waiting, counting }; 
 
     [System.Serializable]
     public class Wave
     {
         public string name; 
         public GameObject []  enemy;
         public int count;
         public float rate;
     }
 
     public Wave [] Waves;
     private int nextWave = 0;
 
     public Transform[] spawnpoints;
 
     public float TimeBetweenWaves = 5f;
     private float wawecountdown;
 
     private float Searchcountdown = 1f;
 
     private SpawState state = SpawState.counting;
 
     void Start ()
     {
         if (spawnpoints.Length == 0)
         {
             Debug.LogError ("We need SpawPoints");
         }
 
 
         wawecountdown = TimeBetweenWaves;
     }
 
     void Update ()
     {
         if (state == SpawState.waiting) 
         {
             if (!Enemyisalive ()) 
             {
                 WaveComploted ();
             } 
             else
             {
                 return;
             }
         }
         if (wawecountdown <= 0) 
         {
             if (state != SpawState.Spawning)
             {
                 StartCoroutine (SpawWave (Waves [nextWave]));
             }
         }
         else 
         {
             wawecountdown -= Time.deltaTime;    
         }
     }
 
 
     void WaveComploted ()
     {
         Debug.Log ("WaveCompleted");
 
         state = SpawState.counting;
         wawecountdown = TimeBetweenWaves;
         if (nextWave + 1 > Waves.Length - 1) {
             nextWave = 0;
             Debug.Log ("We comploted all waves");
         } 
         else
         {
             nextWave++;
         }
 
 
     }
 
 
     bool Enemyisalive ()
     {
         Searchcountdown -= Time.deltaTime;
         if ( Searchcountdown <= 0f)
         {
             Searchcountdown = 1f;
             if (GameObject.FindGameObjectWithTag ("Enemy") == null)
             {
                 return false;
             }
 
         }
         return true;
     }
 
     IEnumerator SpawWave (Wave wave)
     {
         Debug.Log ("SPaw$$anonymous$$g wawe" + wave.name); 
         state = SpawState.Spawning;
 
         for (int i = 0; i < wave.count; i++) 
         {
             SpawEnemy (wave.enemy);
             yield return new WaitForSeconds (1 / wave.rate);
         }
 
         state = SpawState.waiting;
         yield break; 
     }
     void  SpawEnemy (GameObject enemy)
     {
         Debug.Log ("SPawning enemy" + enemy.name);
 
         Transform sp = spawnpoints [Random.Range (0, spawnpoints.Length)];
         int randomInt = Random.Range(0, 3);
         Instantiate (enemy[randomInt], sp.position, sp.rotation);
 
     }
 }
 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Does c# scripts take up more space than javascript scripts? 1 Answer

How can i get original Pixel size of texture in C# script? 0 Answers

Pinpointing one vertice with RaycastHit 3 Answers

When the enemy move between waypoints if a waypoint is too big he will stuck on it how can i solve it ? 0 Answers

How can i put a gameobject in bottom of game screen as selecteable item ? 0 Answers


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