• 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
Question by Jammer3000 · May 21, 2014 at 12:58 AM · javascriptinstantiateclone

Code only affects first clone?

Hi Jeremy here! The code below works great but only for the first clone that is instantiated? I need it to work for all the clones and I have never had this problem before and can't seem to quite understand why? Any help is greatly appreciate thank you (:

 #pragma strict
 
 private var evilCloudClone : Transform;
 var evilCloud : Transform;
 var cloudDiesPosition = -3;
 
 function Start () {
     
     // Instantiate the evilCloud and then add Random positions to its x and y positions
     for (var i : int = 0; i < 10; i++) {
         
         evilCloudClone = Instantiate(evilCloud, gameObject.transform.position + Vector3(Random.Range(5, 20), Random.Range(-4, 4), 0), Quaternion.identity);
 
     }
     
 }
 
 
 function Update () {
     
     RespawnEvilCloud();
 }
 
 
 function RespawnEvilCloud () {
     
     // Determins when to delete evil cloud and respawn
     if (evilCloudClone.transform.position.x < cloudDiesPosition) {
         
         Debug.Log("Cloud Deleted");
         evilCloudClone.transform.position = gameObject.transform.position + Vector3(Random.Range(5, 20), Random.Range(-4, 4), 0);
     }
 
 }
Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Jammer3000 · May 22, 2014 at 12:58 PM

Thanks Good old Grim that was it here is the working code! Thanks to everyone that helped!

 #pragma strict
      
     private var evilCloudClone : Transform;
     private var evilCloudsArray : GameObject[];
     
     var evilCloud : Transform;
     var cloudsResetPositionPosition = -4;
      
     function Start () 
     {
      
         // Instantiate the evilCloud and then add Random positions to its x and y positions to simulate random spawning
         for (var i : int = 0; i < 10; i++) 
         {
            evilCloudClone = Instantiate(evilCloud, gameObject.transform.position + Vector3(Random.Range(5, 20), Random.Range(-4, 4), 0), Quaternion.identity);
         }
          
          // Fill our clouds array with all gameobjects with tag EvilCloud(fills array with all evil cloud gameobjects)
         evilCloudsArray = GameObject.FindGameObjectsWithTag("EvilCloud"); 
     }
      
      
     function Update () 
     {
          RespawnEvilCloud();
     }
      
      
     function RespawnEvilCloud () 
     {
         // Resets position of clouds(array) objects to random position on right side of screen and resets the positions of x and y for randomness
         for (var i : int = 0; i < evilCloudsArray.Length; i++) 
         {
            if (evilCloudsArray[i].transform.position.x < cloudsResetPositionPosition) 
            {
              evilCloudsArray[i].transform.position = gameObject.transform.position + Vector3(Random.Range(5, 20), Random.Range(-4, 4), 0);
            }
         }
      
     }
Comment

People who like this

0 Show 0 · 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

Answer by ASPePeX · May 21, 2014 at 01:37 AM

It looks like it should only be working for the last clone. You are overwriting evilCloudClone in every loop, so when the for loop is done evilCloudClone only holds the last clone. Use an array and you are good.

Comment
Kiwasi
Good-old-Grim
Jammer3000

People who like this

3 Show 4 · 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 Jammer3000 · May 21, 2014 at 02:05 PM 0
Share

Thanks ASPePeX what do you mean use an array? For like the the evil cloud clone?

avatar image Good-old-Grim · May 21, 2014 at 02:14 PM 2
Share

Yes. You instantiate 10 clones, but only have one reference. The other 9 are just floating there. You need to store all 10 references in an array and then iterate through that array in RespawnEvilCloud(), calling the code for each clone.

avatar image Jammer3000 · May 21, 2014 at 03:38 PM 0
Share

Is there anyway I could get a example of how this would be done? This is how I tried doing it but it doesn't affect anything? I also assigned the object being instantiated a tag of EvilCloud which should give all its clones that tag too.

 #pragma strict
 
 private var evilCloudClone : Transform;
 var evilCloud : Transform;
 var cloudDiesPosition = -3;
 public var clouds : GameObject[];
 
 function Start () {
     
     // Instantiate the evilCloud and then add Random positions to its x and y positions
     for (var i : int = 0; i < 10; i++) {
         
         evilCloudClone = Instantiate(evilCloud, gameObject.transform.position + Vector3(Random.Range(5, 20), Random.Range(-4, 4), 0), Quaternion.identity);
     }
     
     clouds = GameObject.FindGameObjectsWithTag("EvilCloud");
     Debug.Log("Found Objects with tag EvilCloud");
     
 }
 
 
 function Update () {
     
     RespawnEvilCloud();
 }
 
 
 function RespawnEvilCloud () {
     
     // Determins when to delete evil cloud and respawn
     for (var i : int = 0; i < clouds.Length; i++) {
     Debug.Log("Ran loop");
         if (evilCloudClone.transform.position.x < cloudDiesPosition) {
             
             Debug.Log("Cloud Deleted");
             evilCloudClone.transform.position = gameObject.transform.position + Vector3(Random.Range(5, 20), Random.Range(-4, 4), 0);
         }
     }
 
 }
avatar image Good-old-Grim · May 22, 2014 at 08:09 AM 1
Share

In the second loop, you are using the same old reference. evilCloudClone still holds just one cloud. I don't use javascript so its hard for me to give a proper code example, but I think you need to use clouds[i] in the second loop instead of evilCloudClone.

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.

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

21 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

Related Questions

Instantiating a random dropped consumable item from many cloned objects 1 Answer

How to instantiate 2D object at fixed location? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

OnTriggerEnter2D Rapid Fire 0 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