- Home /
Can I get one array to manage both an object and its script simultaneously?
At the moment I'm using two arrays; one for the cubes I'm managing and one for a script inside of them. Is it possible to get this down to one array only?
private var cubes : GameObject[]; private var cubeScripts : boxmover[]; // The script in question
private var i : int; private var j : int; function Awake () { cubes = GameObject.FindGameObjectsWithTag ("cube"); for (i = 0 ; i<cubes.length; i++) { cubeScripts[i] = cubes[i].GetComponent(boxmover); } }
function Update() { for (i = 0 ; i<cubes.length; i++) { cubeScripts[i].DoUpdate(); } }
Why are you even bothering to ask this? You can't possibly think that by having one array ins$$anonymous$$d of two, you'll somehow increase the performance of your game drastically...? Even if you had one array ins$$anonymous$$d of two, you would still need to manage the references somehow, and it would take the same amount of memory, but again, all this is so insignificant that it really doesn't matter.
I don't expect a performance increase, but I thought having it in one array would make it easier to manage. If it doesn't make much difference then I'll continue as is. Thank you for your comment.
Spike I think that your comment is overly harsh and the question is not bad at all - upvote.
@Billamu: I assume you are just showing an excerpt of your code? Otherwise, if all you're going to do in Update() is calling a cubeScripts.DoUpdate(), I'd completely get rid of that script, and simply move the content of cubeScripts.DoUpdate() to cubeScripts.Update(), which will be called by Unity anyway every frame for every cubeScript.
@Wolfram: I'm developing for Unity iPhone. The script is organising all things that need to be updated through one Update() routine. The idea came from John Grden's speech at the Unity Developer Conference 09 regarding his development of Star Wars: Trench Run.
Answer by Eric5h5 · Aug 06, 2010 at 02:57 AM
I would make the "cubes" array local to the Awake function, and use cubeScripts.length in Update. You only need the cubes array for FindGameObjectsWithTag; no point keeping it around otherwise.
Alternately, if the "boxmover" scripts are only attached to those tagged objects anyway, you can use FindObjectsOfType, which would indeed use just one array.
Thanks for that. I hadn't thought of using FindObjectsOfType. Just to be clear; if I was also referencing the gameObject itself I would need the two arrays?
You can always access the game object to which a script is attached to by using cubeScripts.gameObject - no need to remember them in separate array.
@Wolfram, yes of course! For ages I actually thought the GameObject was the parent of the script, but the script is a Class, meaning the Class acts like a parent to the GameObject. One array pointing to the scripts would be enough. Thank you!
Answer by cncguy · Aug 06, 2010 at 03:06 AM
Class CubeWrapper { var cubeGO : GameObject; var script : CubeScript; function CubeWrapper(in : GameObject) { cubeGO = in; script = in.GetComponent(CubeScript); }
}
create an array of those and access the public variables.
I think this is what I was looking for... I'm still getting my head around Unity and haven't ventured into using classes. This example is easy to understand. Thank you!
I use this method all the time. It is particularly useful if there are a number of components or scripts you want to access on each gameObject. just add whatever public variables you want and initialize them in the constructor. Saves having lots of arrays of references.
Your answer
Follow this Question
Related Questions
Access a String array in one script from another script 0 Answers
Problem with moving objects in array? 2 Answers
Array problem -3 Answers
Increasing And Decreasing An Array Through A Variable 2 Answers