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 RoKo · Oct 17, 2012 at 11:42 PM · instantiate

script is instantiating many copies instead of one

Hey everyone! I'm still learning so please forgive any stupid mistakes.

I have a script here that spawns a prefab "ActivePiece" that translates in direction. This prefab is a GO with a handful of child objects called "unitActive"- these child objects have "collisionScript" attached to them to detect collision.

When the collision is detected what is SUPPOSED to happen is that the children in the prefab ActivePiece are supposed to unparent, have their position rounded to the nearest integers, then a new object "unitIdle" is instantiated at the position of each of these children. Once the new objects have been instantiated the prefab is destroyed and a new one is spawned, restarting the cycle.

I am running into 2 problems.

Problem A- when collision is detected the new unitIdle objects seem to instantiate at the location (rounded to the integer) of the parent in the prefab rather than at each childs location, from what I can tell.

Problem B- after the first prefab is destroyed and its children have been replaced by the new unitIdle objects (although improperly), rather than spawning a single new prefab it continuously spawns new prefabs AND it also continues to spawn unitIdles, but now at the location where the prefab spawns.

I can't figure this one out with google, so I'm hoping you smart folks can shed some light on this.

 #pragma strict
 
 var activePce : GameObject[];
 internal var destThis : GameObject;
 internal var dest : boolean = false;
 var idleUnit : GameObject;
 
 // function to spawn a new game piece
 function spawn ()
 {
     var clone : GameObject;
     
     clone = Instantiate(activePce[(Random.Range(0, activePce.Length-1))], transform.position, transform.rotation);
     clone.name = "ActivePiece";
     destThis = clone;
 }
 
 // spawn initial piece
 spawn();
 
 
     
 function Update ()
 {
     //check for active piece and spawn one if one is not active
     var unitList : GameObject[];
     
     if (dest == true)
     {
         dest = false;
         spawn();
     }
     
     //detect collision    
     unitList = GameObject.FindGameObjectsWithTag("ActiveUnit");
         
     for (var obj : GameObject in unitList)
     {
         var cs : collisionScript = obj.GetComponent(collisionScript);
             
         if (cs)
         {
             if (cs.hit)
             {
                 dest = true;
             }
         }
     }
         
     //if collision is detected- destroy active piece and replace it with idle units
     if (dest)
     {    
         for (var obj : GameObject in unitList)
         {
             var swapUnit : GameObject;
             var pos : Vector3;
     
             obj.transform.parent = null;
             pos = obj.transform.position;
             pos = new Vector3(Mathf.Round(pos.x), Mathf.Round(pos.y), Mathf.Round(pos.z));
             obj.transform.position = pos;
     
             swapUnit = Instantiate(idleUnit, transform.position, obj.transform.rotation);
         }
         Destroy (destThis);
     }
 }
Comment
Add comment · Show 3
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 Eric5h5 · Oct 18, 2012 at 12:34 AM 0
Share

Don't do this, it doesn't work right:

 (Random.Range(0, activePce.Length-1))

See the docs for Random.Range (the integer version). Also, using Update seems like a bad idea. There's no reason to constantly poll for collision when Unity has an OnCollisionEnter callback. Doing FindGameObjectsWithTag every frame is not great for performance either.

avatar image AlucardJay · Oct 18, 2012 at 10:14 PM 0
Share

You can convert this to a comment. click on more, then convert to comment =]

avatar image RoKo · Oct 18, 2012 at 10:14 PM 0
Share

I figured writing this here would be easier than in a comment...

Thanks for the response Eric. There are reasons that I am using raycasting and not actual colliders to detect my collision, but I did take your advice and re-worked the code a bit to increase efficiency. It also seems to have fixed some of the problems.

The script no longer causes things to be instantiated continuously. However- each prefab should spawn 4 "idleUnits" upon collision at the location that its children existed just before its destroyed. However, upon collision it sometimes spawns the proper 4 "idleUnits" BUT sometimes it spawns 8. Also, all of the "idleUnits" that are instantiated are being instantiated at (0,0,0) rather than the position of their respective child piece. Does this make sense?

Here are the current scripts:

collisionScript

 #pragma strict
 
 function Update () 
 {
     var dir = Vector3.forward;
 
     
     if (Physics.Raycast(transform.position, dir, .75))
     {
         var spawner : GameObject;
         spawner = GameObject.Find("spawnerNull");
         
         Debug.Log("hit!!!!!");
         spawner.GetComponent(spawnScript).newPiece();
         Destroy(gameObject);
 
     }
     
 }

spawnScript

 #pragma strict
 
 var activePce : GameObject[];
 internal var destThis : GameObject;
 var idleUnit : GameObject;
 
 // function to spawn a new game piece
 function spawn ()
 {
     var clone : GameObject;
     
     clone = Instantiate(activePce[(Random.Range(0, activePce.Length))], transform.position, transform.rotation);
     clone.name = "ActivePiece";
     destThis = clone;
 }
 
 // spawn initial piece
 spawn();
 
 
     
 function newPiece ()
 {
     //check for active piece and spawn one if one is not active
     var unitList : GameObject[];
 
     //populate array    
     unitList = GameObject.FindGameObjectsWithTag("ActiveUnit");
         
     //replace active units with idle units
     for (var obj : GameObject in unitList)
     {
         var swapUnit : GameObject;
         var pos : Vector3;
     
         pos = obj.transform.position;
         Debug.Log(pos);
         pos = Vector3($$anonymous$$athf.Round(pos.x), $$anonymous$$athf.Round(pos.y), $$anonymous$$athf.Round(pos.z));
         obj.transform.position = pos;
     
         swapUnit = Instantiate(idleUnit, transform.position, obj.transform.rotation);
         Destroy(obj);
     }
     
     Destroy (destThis);
     
     spawn();
     
 }

0 Replies

· Add your reply
  • Sort: 

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Creating an object and setting fields before object begins running 1 Answer

Instantantiate relative to parents position 1 Answer

AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 Answers

How to call a MainClass Funktion ? 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