• 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 /
This question was closed Jul 21, 2014 at 02:05 PM by RedDevil for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by RedDevil · Jul 21, 2014 at 09:00 AM · instantiateprefabsobjects

Instantiate sometimes creates more then 1 objects

I am trying to create 1 point at a time every few seconds on a random basis of 1-5 seconds and the problem im curently facing is that i am sometimes creating 2 objects at the same time even after i added a bool to make sure the instantiate code is only ran once everytime. Here is my code:

  if(only1 == true)
         {
             GameObject Point1 = Instantiate(PointArray[(Random.Range(0, PointArray.Length))], PointSpawner1.position, Quaternion.identity) as GameObject;
             Point1.rigidbody2D.velocity = transform.TransformDirection(new Vector2(-1, 0) * 5);
             only1 = false;
         }
         yield return new WaitForSeconds(Random.Range (1,5));
         if(only2 == true)
         {
             GameObject Point2 = Instantiate(PointArray[(Random.Range(0, PointArray.Length))], PointSpawner2.position, Quaternion.identity) as GameObject;
             Point2.rigidbody2D.velocity = transform.TransformDirection(new Vector2(-1, 0) * 5);
             only2 = false;
         }
         yield return new WaitForSeconds(Random.Range(1,5));
         only1 = true;
         only2 = true;

This code is ran in a loop forever.Please help me.Thank you

Comment
Add comment · Show 40
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 SirCrazyNugget · Jul 21, 2014 at 09:18 AM 1
Share

Are you sure this script is only attached to one object?

avatar image gjf · Jul 21, 2014 at 09:30 AM 1
Share

that only1/2 stuff is unnecessary. the code won't create more than one at a time and will wait for between 1 and 5 seconds... i created a test script as proof. as mentioned, maybe you have it attached to more than one object.

 using UnityEngine;
 using System.Collections;
  
 public class SingleInst : $$anonymous$$onoBehaviour
 {
     public GameObject gameObj1, gameObj2;
 
     private Vector3 pos1 = new Vector3(0,0,0);
     private Vector3 pos2 = new Vector3(1,0,0);
 
     public void Awake()
     {
         StartCoroutine(GenerateObjects());
     }
 
     private IEnumerator GenerateObjects()
     {
         while (true)
         {
             GenerateObj(gameObj1, pos1);
             yield return new WaitForSeconds(Random.Range(1, 5));
 
             GenerateObj(gameObj2, pos2);
             yield return new WaitForSeconds(Random.Range(1, 5));
         }
     }
 
     private void GenerateObj(GameObject obj, Vector3 pos)
     {
         var point = Instantiate(obj, pos, Quaternion.identity) as GameObject;
 
         if (point != null)
         {
             point.rigidbody2D.velocity = transform.TransformDirection(new Vector2(-1, 0) * 5);
         }
         
         Debug.Log(Time.time);
     }
 }


avatar image RedDevil · Jul 21, 2014 at 10:06 AM 0
Share

i checked twice and the script is on only one object the main camera. the script works good at first but after a while it generates like 2 objects on top of each other

avatar image RedDevil · Jul 21, 2014 at 10:09 AM 0
Share

i think this is a glitch or a bug because i tryed again and it worked perfectly

avatar image RedDevil · Jul 21, 2014 at 10:21 AM 0
Share

and it happend again it instaniates random but on top of each other and this should not happend because i have 2 spawn positions

avatar image gjf · Jul 21, 2014 at 10:24 AM 0
Share

but after you've instantiated 2 objects, the next will be over one that's already there. how many objects in your PointArray?

avatar image RedDevil · Jul 21, 2014 at 10:27 AM 0
Share

it wont be because im adding velocity to my instantiated objects making them move

avatar image RedDevil · Jul 21, 2014 at 10:29 AM 0
Share

ohhh... i have 10 objects in the array(3 objects but im puting more of one kind so higher chance of getting those)

avatar image RedDevil · Jul 21, 2014 at 10:32 AM 0
Share

why would the next one after the 2 be on top of an existing one if they apear on spawn point ?

avatar image gjf · Jul 21, 2014 at 10:35 AM 0
Share

if you're moving them, they shouldn't. are you sure they're moving?

try adding in some Debug.Log()'s to see when things are Instantiated and their positions...

avatar image RedDevil · Jul 21, 2014 at 10:41 AM 0
Share

all instantiated objects have rigidbodys and are moving corectly my only problem is that sometimes i find 2 in the same spot moving on top of each other and are diferent or same(so random works)

avatar image gjf · Jul 21, 2014 at 10:44 AM 0
Share

is it possible that one is Instantiated and moves to a spawn point where another is, giving the appearance that it's spawning 2?

did you log the time when each is spawned? that'll prove whether Instantiate is still the problem.

avatar image RedDevil · Jul 21, 2014 at 10:50 AM 0
Share

yes i have done some logs and it is working as it should spawning them 1-5 seconds.

avatar image gjf · Jul 21, 2014 at 10:53 AM 0
Share

so, it's working as written?

avatar image RedDevil · Jul 21, 2014 at 10:55 AM 0
Share

yes but when im watching the spawnpoint i see 2 instantiated sometimes not always

avatar image RedDevil · Jul 21, 2014 at 11:01 AM 0
Share

I now added a wait for seconds before the first instantiate and i see it happening much less and im happy with taht for now thank you for taking the time to help me

avatar image drudiverse · Jul 21, 2014 at 11:03 AM 0
Share

did you print variables earlier in the event chain to see where things can be confused? Unless there is a frame processing problem, i.e. variables dont have time to change between frames, it can only be a variable assignment error.

avatar image RedDevil · Jul 21, 2014 at 11:06 AM 0
Share

after i gave the delay before the first instantiate this problem seems to be gone i played the game a few times and i did not see it anymore

avatar image RedDevil · Jul 21, 2014 at 11:08 AM 0
Share

omg i just noticed now... my code is set to instantiate on spwner 1 and 2 and 90% are on spawner 1 for some reason so there is an instantiate problem here...

avatar image gjf · Jul 21, 2014 at 11:17 AM 0
Share

the code that you posted alternates between the 2 positions. maybe post your updated code.

avatar image RedDevil · Jul 21, 2014 at 11:21 AM 0
Share

i am still using this but there are obvious problem here because it only rearly generate on 2nd position ...

  yield return new WaitForSeconds(Random.Range(2,5));
         GameObject Point1 = Instantiate(PointArray[(Random.Range(0, PointArray.Length))], PointSpawner1.position, Quaternion.identity) as GameObject;
         Point1.rigidbody2D.velocity = transform.TransformDirection(new Vector2(-1, 0) * 5);
         yield return new WaitForSeconds(Random.Range (2,5));
         GameObject Point2 = Instantiate(PointArray[(Random.Range(0, PointArray.Length))], PointSpawner2.position, Quaternion.identity) as GameObject;
         Point2.rigidbody2D.velocity = transform.TransformDirection(new Vector2(-1, 0) * 5);
         yield return new WaitForSeconds(Random.Range(2,5));
avatar image RedDevil · Jul 21, 2014 at 11:29 AM 0
Share

if im using debug.log and typing at start at middle and at end the time values im getting this: at start(0),at middle(5.09),at end(7) at start(7) etc

avatar image gjf · Jul 21, 2014 at 11:29 AM 0
Share

can you print the contents of PointArray and PointSpawner1/2 positions

avatar image RedDevil · Jul 21, 2014 at 11:37 AM 0
Share

ok so for point spawner 1:(8.5, 3.7, -4.0),point spawner2:(8.5, 3.0, -4.0) and for the array:normalpoint,highpoint,megapoint

avatar image gjf · Jul 21, 2014 at 11:45 AM 0
Share

1) you said the array had 10 entries but you posted 3.

2) what are values of the normalpoint, etc.

i'm trying to recreate your problem so need the exact info.

avatar image RedDevil · Jul 21, 2014 at 11:48 AM 0
Share

ok so it has 10 elements 0-5 normal points,6-9 high point and last one mega point

avatar image RedDevil · Jul 21, 2014 at 11:49 AM 0
Share

normaly it should spawn 1 point on top next on bottom at diferent speeds but it spawns sometimes 2 on top or 3 on bottom ins$$anonymous$$d of one on the top and one on the bottom

avatar image gjf · Jul 21, 2014 at 12:38 PM 1
Share

i'm not understanding what you expect this to do versus what it is doing. the code performs as written. maybe you can post some screenshot of what you see (showing the incorrect spawning). my tests with the info you gave all do what they're supposed to.

can you print all of the debug log for each spawn... this modified code will do it, along with showing gizmos on the spawn points (they're fairly close to each other)

 using UnityEngine;
 using System.Collections;
  
 public class SingleInst : $$anonymous$$onoBehaviour
 {
     private readonly Vector3 _pos1 = new Vector3(8.5f, 3.7f, -4.0f);
     private readonly Vector3 _pos2 = new Vector3(8.5f, 3.0f, -4.0f);
 
     public GameObject NormalPoint, HighPoint, $$anonymous$$egaPoint;
 
     private GameObject[] _pointArray;
 
     public void Awake()
     {
         _pointArray = new [] {
             NormalPoint, NormalPoint, NormalPoint, NormalPoint, NormalPoint,
             HighPoint, HighPoint, HighPoint, HighPoint,
             $$anonymous$$egaPoint
         };
 
         StartCoroutine(GenerateObjects());
     }
 
     private IEnumerator GenerateObjects()
     {
         while (true)
         {
             GenerateObj(_pos1);
             yield return new WaitForSeconds(Random.Range(1, 5));
             
             GenerateObj(_pos2);
             yield return new WaitForSeconds(Random.Range(1, 5));
         }
     }
 
     private void GenerateObj(Vector3 spawnPos)
     {
         var point = Instantiate(_pointArray[(Random.Range(0, _pointArray.Length))], spawnPos, Quaternion.identity) as GameObject;
 
         if (point != null)
         {
             point.rigidbody2D.velocity = transform.TransformDirection(new Vector2(-1, 0) * 5);
             Debug.Log(Time.time + " at " + point.transform.position);
         }
         else
         {
             Debug.Log("Instantiated failed!");
         }       
     }
 
     public void OnDrawGizmos()
     {
         AddDebugGizmoSphere(_pos1, 0.3f, Color.red);
         AddDebugGizmoSphere(_pos2, 0.3f, Color.green);
     }
 
     private void AddDebugGizmoSphere(Vector3 pos, float radius, Color color)
     {
         Gizmos.color = color;
         Gizmos.DrawSphere(pos, radius);
     }
 }
avatar image RedDevil · Jul 21, 2014 at 12:45 PM 0
Share

alt text

here you see these 2 points spawning way to close to each other and sometimes they are on top of each other

spawned.png (11.3 kB)
avatar image RedDevil · Jul 21, 2014 at 12:49 PM 0
Share

i did the spawners again and even if they apear to spawn on the same place on the Debug.Log it shows them spawning perfectly each time on spawner1 and spawner2

avatar image RedDevil · Jul 21, 2014 at 12:55 PM 0
Share

between the time it printed the perfect values i noticed it spawned 2-3 more before printing the next ones

avatar image RedDevil · Jul 21, 2014 at 12:57 PM 0
Share

or it prints the transform at 3.0 and spawns it at 3.7

avatar image gjf · Jul 21, 2014 at 01:34 PM 0
Share

at this point, i'd have to see a lot more of your code and proj settings, etc. because i'm confident the bits i've seen work fine.

avatar image RedDevil · Jul 21, 2014 at 01:40 PM 0
Share
  using UnityEngine;

using System.Collections;

public class PointSpawner : $$anonymous$$onoBehaviour { public GameObject[] PointArray; public Transform PointSpawner1; public Transform PointSpawner2;

 void Start () 
 {
     StartCoroutine(SpawnPoints());
 }

 IEnumerator SpawnPoints()
 {
     while(true)
     {
         yield return new WaitForSeconds(Random.Range(2,5));
         GameObject Point1 = Instantiate(PointArray[(Random.Range(0, PointArray.Length))],PointSpawner1.position, Quaternion.identity) as GameObject;
         Point1.rigidbody2D.velocity = transform.TransformDirection(new Vector2(-1, 0) * 5); 
         yield return new WaitForSeconds(Random.Range (2,5));
         GameObject Point2 = Instantiate(PointArray[(Random.Range(0, PointArray.Length))],PointSpawner2.position, Quaternion.identity) as GameObject;
         Point2.rigidbody2D.velocity = transform.TransformDirection(new Vector2(-1, 0) * 5); 
         yield return new WaitForSeconds(Random.Range(2,5));
     }
 }

}

This is the whole code for the point spawner.I put this script on the $$anonymous$$ain Camera ill post below a picture of what i have in the inspector for the script

avatar image RedDevil · Jul 21, 2014 at 01:41 PM 0
Share

alt text

maincamera.png (35.3 kB)
avatar image gjf · Jul 21, 2014 at 01:52 PM 0
Share

i still can't see anything wrong.

i sent you a pm. click on the forums then in the top right you should find a link to your inbox.

avatar image RedDevil · Jul 21, 2014 at 01:53 PM 0
Share

um after just a few hours i think i found the problem ... and it is kinda frustrating...i was moving the points via the animation and not via velocity so ... derp

avatar image RedDevil · Jul 21, 2014 at 01:56 PM 0
Share

and i was controlling the animation via a script on each of the point objects

avatar image gjf · Jul 21, 2014 at 01:56 PM 0
Share

oops ;) so, all ok?

avatar image RedDevil · Jul 21, 2014 at 01:57 PM 1
Share

yes.... and i wasted so much time with this thank you :)

1 Reply

  • Sort: 
avatar image
0

Answer by drudiverse · Jul 21, 2014 at 11:06 AM

You PointSpawner1 and 2 positions are not Random.

They are obviously drawing from the same source of positions and sometimes have the same.

what are pointspawnder1 and 2 code?

Comment
Add comment · Show 1 · 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 RedDevil · Jul 21, 2014 at 11:11 AM 0
Share

i have 2 transforms set point spawner 1 is on a high position and point spawner 2 is on a low position ...but it seems like most are spawning on a top position even tho the code says you spawn from first wait and spawn from second

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy The Same Objects Before Instantiating 2 Answers

A node in a childnode? 1 Answer

setting variable in game object with instantiated object reference 2 Answers

After Build, instantiate does not work 1 Answer

How prefab the instance works? 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