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 Numli · Jul 23, 2014 at 03:11 PM · javascriptinstantiatenewbie

Spawning 2 objects in exactly the same place!!!

Hi All, I have been using unity for about 2 weeks now and I'm fairly new to programming as well so please excuse my ignorance. I am trying to build a simple spawn mechanic that will spawn objects and send them towards the player. It appears to be working but I have noticed that sometimes there will be 2 objects in exactly the same place. This wouldn't normally be a problem but it is causing my die() method to be triggered twice which causes life counting problems, here is my spawn script any help would be much appreciated. If you need any more information I will do my best to provide it.

 #pragma strict
 var block1 : GameObject; 
 var scriptJunky: Transform;
 var rand: int;
 var xSpawn: int;
 var xSpawnOld: int;
 var k :int;
 var xSpawns = new Array();
 var xSpawnsFloat: float[];
 var x: int = 0;
 var clone : GameObject;
 var startblock: GameObject;
 
 function Start () {
 xSpawnsFloat = [-2,-1.5,-1,-0.5,0,0.5,1,1.5,2];
 InvokeRepeating("SpawnBlockTimer", 0, 0.6);
 xSpawnOld = 1;
 Random.seed = System.DateTime.Now.Second;
 
 
 }
 
 function Update(){
         
             xSpawn = Random.Range(0,9);
 }
 
 
 function SpawnBlockTimer() {
 
     var restartScript: RestartGUI = scriptJunky.GetComponent("RestartGUI") as RestartGUI;
     
     if(restartScript.isPaused == false){
     
             if(xSpawn < xSpawnOld){
                 xSpawnOld = xSpawn + 3;
                 SpawnBlock();
                 Debug.Log("SpawnBlock < Triggered" + System.DateTime.Now);
             }else if (xSpawn > xSpawnOld){
                 xSpawnOld = xSpawn - 3;
                 SpawnBlock();
                 Debug.Log("SpawnBlock > Triggered" + System.DateTime.Now);
             }else if (xSpawn == xSpawnOld){
                 xSpawn = Random.Range(0,9);
         
             }
     
         }
             
             
     }
 
 
 function SpawnBlock(){
                 
                 
                 clone = Instantiate(block1, Vector3(xSpawnsFloat[xSpawnOld],0.45,20), block1.transform.rotation);
                 //Render it visable
                 var mesh : MeshRenderer = clone.GetComponent(MeshRenderer);
                 mesh.enabled = true;
                 
                 //Destroy after x seconds
                 Destroy (clone, 3);
                 
                 if(clone.activeSelf == true){
                 clone.transform.tag = "Enemy";            
                 }
                 
                 var enemies = GameObject.FindGameObjectsWithTag("Enemy");    
                 
                 //Check how many obsticles are alive    
                     if(enemies.Length > 8){
                     Destroy(clone);
                     }
                 yield WaitForSeconds(0.5);        
 }




Thank you in advance!!!!

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
Best Answer

Answer by Tehnique · Jul 23, 2014 at 03:20 PM

Well, there are multiple solutions:

  • Add a collider (& rigidbody) to your objects to stop them from intersecting eachother. This might give weird results if 2 are spawned in the same place.

  • Remember where you spawned your last object, and if the new position is the same, get a new position.

  • The one I'd recommend: Fix your "die" method, so after an object hits your player, you set 1-2 seconds for the player to be invulnerable, so even if a second enemy hits him, life counters don't go down. You can just use a bool and the Invoke method for this (have a bool like "isRespawning", set it to true on the first hit and call an Invoke method that sets it to false after 2 sec. When you are hit, check that "isSpawning" is false, if it is true, do nothing). I don't know what game you are building, but lots of games have this mechanic.

Comment
Add comment · Show 2 · 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 Numli · Jul 24, 2014 at 07:57 AM 0
Share

Thank you very much, I have just tried your recommended fix and it appears to have worked perfectly. Just out of curiosity, do you have any idea why it might be spawning duplicates? as my spawn method should only be called every 0.6seconds anyway? Thank you again!

avatar image Tehnique · Jul 24, 2014 at 08:16 AM 0
Share

Well, your method is not called every 0.6 seconds, it just takes 0.6 seconds to execute, it can be called 100 times, and 0.6 seocconds later you will have 100 spawns. Take another look at coroutines, use a IEnumerator return type on your spawn method, use a yield return statement (an example here), and most importantly, set a bool, like "isSpawning" that is set to true when you enter the method, and false after your yield statement in the method. From the outside, call the Spawn method ONLY if "isSpawning" is false.

This should fix it.

Also, if you do what is described in this comment, there is no need for the Invoke and isSpawning bool from the answer. You chose between the 2 solutions (the one in this comment would be cleaner imo).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiate position from four options. 1 Answer

Instanitiate PreFabs and let them move to different locations 1 Answer

How do you Instantiate a Prefab and call its instance variable? 1 Answer

Attach script twice. 1 Answer

Doesn't Instantiate at the assigned EmptyObject 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