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 /
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 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 01:57 PM 1
Share

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

Show more comments

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

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

Invisable Projectile; Instantiate at Collisions 2 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