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 Tsailen · Aug 01, 2012 at 12:04 AM · javascriptarray

Can i use objects from a builtin Array to populate a Js Array

I very little scripting and/or programming experiuence, but i am a quick study. This is my first real work with array, so i dont fully understand the concepts yet, but im getting there.

using Unity Script

What i am trying to do is dynamically create an array from objects stored in a built in array. I want the the new array to add objects that meet certain conditions. From this array, one of the objects will be 'randomly' selected for instantiation.


example:

-Built in array ARR contains objects A, B and C.

-each object has property X.

-Build array New by adding ojects from ARR with an X value above 10.

-get resulting size of array New, and use that as the max value in a random range


Is this possible? Is it the right way to acheive my end result?

thanks

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Aug 01, 2012 at 12:07 AM

Did you read the manual? http://docs.unity3d.com/Documentation/ScriptReference/Array.html Let us know if that doesn't answer your question.

Comment
Add comment · Show 3 · 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 Tsailen · Aug 01, 2012 at 12:57 AM 0
Share

in short, it does not answer my question in a way i understand. :)

yep. i read all of that, But i dont understand it yet. not even sure if it says this is or is not possible... When i said very little experience, i meant little to no experiences. I am an artist trying to learn some basic scripting to prototype some ideas.

This may just simply be beyond my current level of mismatched understanding. jumped ahead too far...maybe time to step back to basics. :)

avatar image DaveA · Aug 01, 2012 at 01:24 AM 0
Share

Yeah, those examples on that page are pretty close to what you need. $$anonymous$$ore clues here: http://docs.unity3d.com/Documentation/ScriptReference/Array.Array.html

Untested:

// Creates an empty array var newArr = new Array ();

//Now loop through original array, adding only what you need for (var o in ARR) { if (o.X > 10) // Add element to new array newarr.Push (o); } // Copy the js array into a builtin array var NEW : GameObject[] = newarr.ToBuiltin(GameObject);

avatar image Tsailen · Aug 01, 2012 at 09:07 PM 0
Share

Awesome. While you above example was not the whole answer, it provided enough to steer me in the right direction. After scouring the ref manual, and numerous other Answer pages, i think i finally came up with a working example. I have a little more testing to do to make sure. Once i get it all working, i'll post the result for your viewing pleasure (or displeasure due to my amature code!).

avatar image
0

Answer by Tsailen · Aug 02, 2012 at 06:34 AM

After a bit of trial and error, i came up with something that worked. This is the pertinent part of the script afer cleanup. I have tested it and it does what i want. Forgive the amature code. If anyone sees a better way to do any of this, or see something i missed, let me know.

 var objArr : GameObject[];
 var lvlProg = 0;
 var maxVal : int;
 var instAr = new Array();

 function Awake() {
 
   lvlProg = GameStart.lvlProg;
   var objTbl = new Array();

   for (var obj : GameObject in objArr){
   var oWeight = obj.GetComponent(ObPrp);
   if (oWeight.weight > lvlProg){

objTbl.Push(obj); } } }

 function Start(){
   instAr = objTbl;
   maxVal = instAr.length;

   Generate();
 }

 function Generate(){
   var min = 0;
   var max = (maxVal);
   var objGen = Random.Range(min,max);
 
   Instantiate (instAr[objGen], transform.position,transform.rotation);
   GameStart.lvlProg ++;
   Destroy(gameObject);

}

Comment
Add comment · 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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Setting Scroll View Width GUILayout 1 Answer

Can't edit built-in array through script unless viewed in Inspector 1 Answer

Set variable/Array to a length by scripting 2 Answers

Array of custom class objects all return the same value? 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