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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by parjanyaroy · Jul 10, 2013 at 04:40 PM · arrayobjectpoolreuse

Creating an object pool . Facing problem with array

I am creating an endless runner game where i want to reuse the prefab platforms. At first I am copying the drag and dropped prefabs into an array "DEACTIVATED_HOLDER" When the game begins i want to call a function to select a prefab at random from this array and move it to ACTIVATED_HOLDER array after activating the gameobject. I have run into a few problems .Could you please guide me how do i progress ? thnx in advance (:

 #pragma strict
 @script ExecuteInEditMode();
 var array_status : GUIText ;
 
 var prefabPlatform : GameObject[];
 static var deactivated_holder : Array ;
 static var activated_holder  : Array ;
 static var postionMultiplier=0 ;
 var prefabwidth=300;
 static var check=0;
 
 
 function Start () {
 deactivated_holder=new Array ();
 var activated_holder = new Array ();
 var new_prefab : GameObject ;
 for(var i=0;i<prefabPlatform.length;i++) // MOVING ALL THE DRAG AND DROPPED PREFABS FROM prefabPlatform to deactivated_holder
 {
 new_prefab=Instantiate(prefabPlatform[i], Vector3(0, 0, postionMultiplier*prefabwidth), Quaternion.identity);
 new_prefab.name="Platform_"+(i+1);
 new_prefab.gameObject.active=false;
 deactivated_holder.Push(new_prefab);
 }
 relocate_prefab();
 }
 
 function Update () {}
 
 function relocate_prefab() // Here i want to fetch a gameObject from the array at random and push it 
                             // into the activated_holder after activating the prefab 
 {
 var z_coordinate_generation=postionMultiplier*prefabwidth ;
 var to_be_activated_prefab=deactivated_holder[Random.Range(0,deactivated_holder.length)];
 print("z:"+z_coordinate_generation+","+to_be_activated_prefab.);
 to_be_activated_prefab.gameObject.active=true;
 to_be_activated_prefab.transform.position(0,0,z_coordinate_generation);
 }
 

I AM NOT ABLE TO FETCH THE GAMEOBJECT FROM DEACTIVATED_HOLDER AND POSITION IT

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

Answer by justinl · Jul 10, 2013 at 04:47 PM

Don't use Array.Push(). Use a List instead (Dynamically re-sizable collection). I don't believe Array.Push() works in Unity the same way it works in normal Javascript so this might be the reason you cannot access the GameObject.

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 parjanyaroy · Jul 10, 2013 at 05:56 PM 0
Share

Thnx ... using list worked !!

 #pragma strict
 import System.Collections.Generic;
 @script ExecuteInEdit$$anonymous$$ode();
 
 var array_status_deactivate : GUIText ;
 var array_status_activate : GUIText ;
 
 var prefabPlatform : GameObject[];
 var deactivated_holder = new List.<GameObject>();  
 var activated_holder = new List.<GameObject>();  
 
 static var postion$$anonymous$$ultiplier=0 ;
 var prefabwidth=300;
 static var check=0;
 
 
 function Start () {
 var new_prefab : GameObject ;
 for(var i=0;i<prefabPlatform.length;i++)
 {
 new_prefab=Instantiate(prefabPlatform[i], Vector3(0, 0, postion$$anonymous$$ultiplier*prefabwidth), Quaternion.identity);
 new_prefab.name="Platform_"+(i+1);
 new_prefab.gameObject.active=false;
 deactivated_holder.Add(new_prefab);
 }
 
 relocate_prefab();
 
 }
 
 function Update () {
 array_status_display();
 }
 
 
 
 
 
 function array_status_display()
 {
 var deactivation_status="DE-ACTIVATED \n";
 for( var element_prefab in deactivated_holder )
 {
   deactivation_status=deactivation_status+element_prefab+"\n" ;
 }
 
 array_status_deactivate.text=deactivation_status;
 
 
 var activation_status="ACTIVATED \n";
 for( var element_prefab in activated_holder )
 {
   activation_status=activation_status+element_prefab+"\n" ;
 }
 
 array_status_activate.text=activation_status;
 
 
 
 }
 
 function relocate_prefab()
 {
 var z_coordinate_generation=postion$$anonymous$$ultiplier*prefabwidth ;
 var position=Random.Range(0,(deactivated_holder.Capacity-1));
 var to_be_activated_prefab=deactivated_holder[position];
 deactivated_holder.RemoveAt(position);
 
 
 activated_holder.Add(to_be_activated_prefab);
 to_be_activated_prefab.gameObject.active=true;
 to_be_activated_prefab.gameObject.transform.position.z=0;
 to_be_activated_prefab.gameObject.transform.position.y=0;
 to_be_activated_prefab.gameObject.transform.position.x=0;
 }
avatar image justinl · Jul 10, 2013 at 06:35 PM 0
Share

Great to hear :)

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

16 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

Related Questions

Enabled all gameobjects in array 1 Answer

Lists and Structs instead of Arrays? 4 Answers

How do I check the distance between multiples objects in an array from the player? 4 Answers

JS: Array index not taking array.length as valid int? 2 Answers

How to make an Array equal the Components of an Instantiated GameObject 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