- Home /
Create Array of Transforms and Detect Distance
Hi,
i don't understand the doc, i would like to use an ArrayUtility (http://docs.unity3d.com/Documentation/ScriptReference/ArrayUtility.html) function : IndexOf, and they say : To use it you have to place your script in Assets/Editor inside your project folder.
Is it a window named Asset/Editor? Do i have to create it myself?
Thanks
If my answer below helped please mark this question as Answered Pauls :) Thanks.
Answer by code-blep · Nov 27, 2012 at 09:10 PM
Hi Pauls,
Inside Unity, look for the Project window. In that window see if there is a folder called 'Assets' if so go into the folder and see if there is a folder called 'Editor'. If the folders are not there, create them like any other folder.
When the folder structure is correct, drag and drop the Array Utility into the Editor folder.
Hope this helps.
Paul
@UFOHunter Thanks a lot UFO Hunter, so i will have to create them. And what is Array utility? a file to drag and drop? Does it come with an asset i should import?
Thanks!
I think we need to start at the beginning again ;)
I take it from the question you are looking to use arrays yes? If so we need to get back to what you actual need is. What will you be using your array for?
I will need to fill in 2 arrays, one with gameObjects, and the second with the distance between each gameObject and a ball. So that i can sort the 1st array of gameObjects depending on the second array, (from max to $$anonymous$$) : so basically, if 3 game objects are near the ball, i put them in an array, i put their distance with the ball in another array, and i sort the distanceArray from max to $$anonymous$$, and i would need to know their index to sort the gameObjects array the same way. Then, when i push the "E" button, i can select a gameobject in the array, if i push again the E button, it goes to another gameobject etc. so that i can control any of the three gameObjects, just by switching them with the "E" button. Actually i am a bit confused, because indexOf is not what i would need, would you know how i could achieve this will all my informations? ;-) Thanks!
O$$anonymous$$, it's a lot of stuff to cover here. Are you coding in C# or UnityScript? If it's UnityScript I'll give you some code to get you going, also I start the answer off by posting some links to essential reading to get you going, and will add code later if you are using UnityScript.
Also I would change the title of your Question to something like Create Array of Transforms and Detect Distance, and then tag the question. If you do this then you will generate more $$anonymous$$arma points for yourself, and help other to find solutions ;)
Answer by code-blep · Nov 28, 2012 at 08:11 PM
OK, to start here are some essential links for arrays:
http://wiki.unity3d.com/index.php/Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F
http://docs.unity3d.com/Documentation/ScriptReference/Array.html
http://msdn.microsoft.com/en-us/library/d9hw1as6%28v=vs.90%29.aspx
I strongly recommend that you use ArrayLists, and virtually everyone will agree with this ;)
I'll add some examples now as other may find this useful anyway:
First you need to create an ArrayList to store your GameObjects. You actually need to store them as Transforms.
Create an empty GameObject and call it 'Start'
Create a new JavaScript and call it 'ObjectsArray', attach it to the 'Start' GameObject in your scene
Add the following code to the 'ObjectsArray' script:
//Need this for the ArrayList function enter code here import System.Collections.Generic;
//Create Your Array var allObjects = new List.();
You now have an array in a script called 'ObjectsArray' which you can add Transforms to.
Create a new script and call it 'AddMeToArray'
Add the following code to your script:
//Create a Var so you can refer to the script var objectsArrayScript : ObjectsArray;
function Start() { //Find the script
objectsArrayScript = GameObject.Find("Start").GetComponent(ObjectsArray);//Adds the transform of the object this script is attached to our array objectsArrayScript.allObjects.Add(this.transform); }
You now need to attach the 'AddMeToArray' script to the parent of any GameObject you would like added to the array.
So all being well, then you start the game, each object will be added to the array.
Let me know how you get on with this, and when working we can take it further...
Here is a link to an example project I have put together for you: [1]: /storage/temp/5347-arrays+tutorial.zip
I have updated the code and this forum was doing funky stuff to it's formatting. It should be fine now but keep refreshing just in case ;)
Thanks a lot, it is really nice from you, i'm more familiar with it now. I have also found some interesting stuffs, the main one is i think i should not iterate through all the gameobjects like how i planned to do, because i'm afraid of the performance (+ it is on mobile ;-) ) So i think i will use a physics function like overlap and check the layer attached to each gameobject that is close enough to the ball. Thanks anyway UFO Hunter, your messages were really helpful!
No problem. Don't forget to vote one of these answers as accepted! Good luck!
Answer by Bunny83 · Nov 28, 2012 at 10:35 PM
You got that page completely wrong. The notice at the top tells you that the ArrayUtility class (which is a built in class) belongs to the UnityEditor. It can't be used at runtime / in your game. It can only be used in editor scripts. Editor scripts are scripts that adds more functionality to the Unity editor. Editor scripts have to be placed in a folder called "Editor" inside your Assets folder. The UnityEditor namespace can / should only be used inside editor scripts. When you use this namespace in your normal game scripts you can't build your game since everything in this namespace belongs to the Unity editor which is not available when you ship your game.
Like UFO Hunter said, you should use a List instead. As a side note, the example in UFO Hunter's answer are written in UnityScript (Unity's javascript)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
properly initializing my arrays (C#) 2 Answers
Remove object from List after gameObject was destroyed in between Scene loads 1 Answer
Tree Instance Array Replacement 2 Answers
Sorting Variables Help 1 Answer