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 Chappy · Nov 18, 2013 at 07:08 PM · c#randomspawn

Choosing a random location out of a list for an object to spawn at.

I've got a basic spawning script for enemies but I'm trying to make it so the enemies will spawn at a randomly chosen location out of a set that I input.

     public GameObject[] spawnObject;
     public GameObject[] spawnPosition;
     
     public float radius = 1.0f;
     public float minSpawnTime = 1.0f;
     public float maxSpawnTime = 10.0f;
     public bool constantSpawn = false;
     //public Transform spawnPosition;
     //Vector3 randomLocation;
     public float SpawnCount = 0f;
     public float LookAtDistance = 10.0f;
     public float Distance;
     public Transform Target;
     public float Damping = 5.0f;
     private Quaternion rotation;
     public bool deathspawn = true;
     
     void Start()
     {
         Target = GameObject.FindGameObjectWithTag ("Player").transform;
 
     
         
 
     }
     
     void Update(){
     
         Distance = Vector3.Distance (Target.position + Target.transform.forward, transform.position);
         
         if (Distance < LookAtDistance)
         {
             LookAt();
         }
         
     }
     
     void OnTriggerEnter(Collider Player)
     {
     
             Invoke("SpawnEnemy", Random.Range(minSpawnTime,maxSpawnTime));
         
         
         
     }
     
     void OnTriggerExit(Collider Player)
     {
     
         CancelInvoke ("SpawnEnemy");
         deathspawn = false;
         
     }
     
 
     void SpawnEnemy () 
     {
         
         //randomLocation = Random.insideUnitSphere * radius;
         
         
         //float spawnRadius = radius;
         int spawnObjectIndex = Random.Range(0,spawnObject.Length);
         int spawnPositionIndex = Random.Range (0, spawnPosition);
             
         //transform.position = Random.insideUnitSphere * spawnRadius;
             
         Instantiate(spawnObject[spawnObjectIndex],spawnPosition[spawnPositionIndex].position + randomLocation,spawnObject[spawnObjectIndex].transform.rotation);
         
         SpawnCount += 1f;
         
         if (constantSpawn == true && SpawnCount <= 5)
         {
             Invoke ("SpawnEnemy", Random.Range (minSpawnTime,maxSpawnTime));
             
             
         }
   
         
     }
     
     void LookAt(){
 
     rotation = Quaternion.LookRotation (Target.localPosition - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
         
     }
     
     public void SpawnDown (float reduction){
         
                 SpawnCount -= reduction;
 
                 if (deathspawn == true) {
 
                     Invoke ("SpawnEnemy", Random.Range (minSpawnTime, maxSpawnTime));
                 }
         }
     
 }

I used a random selection for the enemies themselves but to be completely honest I got that section of the code from a tutorial and I am not really sure how everything is implemented into the code. The spawnObject allows me to assign multiple objects to be spawned and I'm asssuming the spawnObjectIndex is what chooses from a random object in that list so I copies that over for the spawnPosition but I'm not sure how to put it into the instantiate function. What I've got now leads to 5 errors. I'm assuming I did something wrong with the spawnPositionIndex () bit and I know I messed up the Instantiate.

Any assistance would be appreciated and if this is unclear please let me know and I'll try to explain it better, it's difficult to describe problems concerning stuff you don't get very well

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by gajdot · Nov 18, 2013 at 07:22 PM

It's a bit chaotic what you did there. So if you don't mind I wont' go trough your script but tell the basic idea how to do it. So you have the random locations in the spawnPosition array, so what you need to do is, just give that to the instantiate function, like this:

 int spawnPositionIndex = Random.Range (0, spawnPosition.Length); // or Count can't remember right, anyway the length of the array list
  Instantiate(spawnObject[spawnObjectIndex],spawnPosition[spawnPositionIndex].transform.position,spawnPosition[spawnPositionIndex].transform.rotation);

edited it so others could have the good code too.

Comment
Add comment · Show 4 · 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 Chappy · Nov 18, 2013 at 07:38 PM 0
Share

Yeah, my coding is definitely not the pretty right now. I'm currently just trying to get the job done, I'll work on refining stuff once this semester ends.

I put in what you suggested but I'm still getting 4 errors, all within the instantiate function.

Assets/Test Scripts/Leech_$$anonymous$$other_AI_Test.cs(73,93): error CS1061: Type UnityEngine.GameObject' does not contain a definition for position' and no extension method position' of type UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)

Assets/Test Scripts/Leech_$$anonymous$$other_AI_Test.cs(73,136): error CS1061: Type UnityEngine.GameObject' does not contain a definition for rotation' and no extension method rotation' of type UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)

Assets/Test Scripts/Leech_$$anonymous$$other_AI_Test.cs(73,17): error CS1502: The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments Assets/Test Scripts/Leech_$$anonymous$$other_AI_Test.cs(73,17): error CS1503: Argument #2' cannot convert object' expression to type UnityEngine.Vector3'

avatar image gajdot · Nov 18, 2013 at 07:52 PM 1
Share

Ohh yeah, the gameobject doesn't have position, nor rotation, you need to write spawnPosition[spawnPositionIndex].transform.position and spawnPosition[spawnPositionIndex].transform.rotation

OR you need to change the spawnPosition array to Transform[] your call

avatar image Chappy · Nov 18, 2013 at 08:01 PM 0
Share

That worked perfectly.

I probably should have realized the solution to that problem myself but again, still learning all the syntax.

avatar image gajdot · Nov 18, 2013 at 08:10 PM 0
Share

I'm glad I could help. Even the experienced coders make silly mistakes and spend hours to figure out that you wrote = in an if statement ins$$anonymous$$d of == :D or something similar :)

Just keep up the good work :) a good programmer learns till he dies :)

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

How to make an object/prefab choose between multiple given positions to spawn into? 1 Answer

Random spawning of objects 4 Answers

move gameobject into a random position and the spawn an enemy 1 Answer

Make a random number get chosen less or more? (C#) 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