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 /
  • Help Room /
avatar image
0
Question by Naquarus · Apr 26, 2016 at 07:09 PM · c#spawningspawning problems

Problem with Random Spawning

This is my code:

 using UnityEngine;
 using System.Collections;
 
 public class Spawn : MonoBehaviour {
     public GameObject[] walkers;
     public int walkeramount;
     public GameObject Walker;
 
     public GameObject[] enemies;
     public Rigidbody Enemy;
     public int enemyamount;
 
     private Vector3 walkerSpawnPoint;
     private Vector3 enemySpawnPoint;
 
     // Update is called once per frame
     void Update () {
         walkers = GameObject.FindGameObjectsWithTag("Walker");
         enemies = GameObject.FindGameObjectsWithTag("Enemy");
         walkeramount = walkers.Length;
         //keeps track of walkers and stores amount.
         if(walkeramount != 20)
         {
             InvokeRepeating("spawnWalker", 5, 10f);
         }
         if(enemyamount < 30)
         {
             InvokeRepeating("spawnEnemy", 3, 3f);
         }
     
     }
     void spawnWalker()
     {
         walkerSpawnPoint.x = Random.Range(-20, 20);
         walkerSpawnPoint.y = 0.5f;
         walkerSpawnPoint.z = Random.Range(-20, 20);
 
         Instantiate(Walker, walkerSpawnPoint, Quaternion.identity);
         CancelInvoke();
     }
     void spawnEnemy()
     {
         enemySpawnPoint.x = Random.Range(-20, 20);
         enemySpawnPoint.y = Random.Range(2, 6);
         enemySpawnPoint.z = 5;
 
         Instantiate(Enemy, enemySpawnPoint, Quaternion.identity);
         CancelInvoke();
     }
 }


Despite the fact the code runs without error, when the enemies do spawn, the z axis says 5 yet I have another object called player that has a z position of 5 also yet does not sit anywhere near to it. Is there a reason why two objects would not be using the same scale of position? (I have checked and it is definitely the z axis and not another axis).

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
0

Answer by Barsonax · Apr 26, 2016 at 07:26 PM

Do the prefabs you are using have the same scale? This might be a cause.

I noticed in your update function you are constantly searching for gameobjects. I think a better system would be to use events to count how many enemies have spawned. I dont think this would matter though performance wise if its a very small game but just so you know. Its pretty bad performance wise to constantly search the scene for gameobjects even if you use tags.

Also unity has some nice build in functions to return random points. Just look in UnityEngine.Random they might suit you more.

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 Naquarus · Apr 26, 2016 at 07:32 PM 0
Share

How would I check what scale the prefabs are using?

avatar image Barsonax Naquarus · Apr 26, 2016 at 07:38 PM 0
Share

Simply go to the folder they are located and select them (inside of unity of course). In the inspector you should be able to see the scale.

Unfortunately it seems i cannot upload a screenshot but it should be easy enough to find. Just look for the transform component in the inspector. It should be in the top right part of your screen.

avatar image Naquarus Barsonax · Apr 26, 2016 at 07:41 PM 0
Share

The scale is 1, 1, 1 for everything.

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

Spawn system that doesn't instantiated enemies on top of player or each other (C#) 2 Answers

How to rotate an object 2 Answers

Items spawn and will not stop spawning. 2 Answers

How to spawn a prefab when a button with a certain tag is pressed 1 Answer

Make a collision check when spawning with Instantiate 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