- Home /
Adding objects to a Array
Alright, I am going to post the code for this script, but since i am working on it, it is VERY messy and slightly confusing. I usually clean scripts up once they start to work.
var spawnPoint : GameObject; var cannonBallPrefab : Rigidbody; var power : float = 1000; var mCamera : GameObject; private var number : int = 1; private var arr : GameObject[];
function Update () { if(Input.GetButtonDown("Fire1") ) { var cannonBall : Rigidbody = Instantiate(cannonBallPrefab, spawnPoint.transform.position, Quaternion.identity); cannonBall.name = "CannonBall" + number; cannonBall.transform.rotation.y = -1; cannonBall.rigidbody.AddForce(cannonBall.transform.forward * -power); var bName : String = "CannonBall" + number; var ballObject : GameObject = GameObject.Find(bName); var cameraScript = mCamera.GetComponent(CameraScrolling); number++; cameraScript.SetTarget (arr[number], true); } }
Like i said it is very messy. The idea here is to fire a cannon when the fire button is clicked. When it fires, it will spawn a cannon ball, and the camera will follow the cannon ball, and watch it hit stuff. Well i got the whole spawning thingy to work (really messy as the transform for it is messed up because the modeler screwed with them). Now the problem is the array that i want to use to track the movement of the cannon ball.
The way to cameraScrolling script is set up (it is the same one used in the 2d Gameplay tutorial made by Unity) is it takes a array, and sets it as the target. So i need to set the newly created cannon ball, to be added to the array, so the script can access it, but it will not work. I have tried many ways but none seem to work. Please help me out, as i am completely lost with arrays.
Answer by duck · May 14, 2010 at 07:21 AM
There are a few different kinds of array available in Unity. The ones defined with square brackets [] are not resizable, so you can't use Add or Remove, or any functions which change the size.
What you probably want is a "Javascript Array", which does have these functions.
For a more detailed description of the various arrays and when you might want to choose each, see this Unity Wiki Article:
@$$anonymous$$arowi probably not much to do with what you wanted (and I don't even like sports that much either) but here's a "ducks fanclub": http://www.goducks.com/ :P
Answer by Extrakun · May 14, 2010 at 07:42 AM
There is no upper limit to the number of cannon balls you can spawn, so you should use a dynamic array, such as ArrayList or a Javascript Array
private var arr : Array;
You would need to initialize the array
function Start()
{
arr = new Array();
}
And to add your cannonball to it, just use Push
arr.Push(cannonBall);
However, if you wish to use fixed native array, there's a couple of things to remember.
- You need to set the size
- You cannot exceed the size of the array
- Indexing starts from 0, not 1, as in your script
So the code might look like:
private var arr : GameObject[]; private var number:int = 0; private var max:int = 100;
function Start() { arr = new GameObject[max]; }
function Update() { // snipped code to spawn cannon-ball
// remembering that array counts from zero if (number < max-1) {
arr[number] = cannonBall;
number++;
} }
couple of notes : indexing starts at zero for all kinds of array in Unity's Javascript and C# (not just fixed size arrays). Also, your code above seems to miss adding an item into position zero!
Thanks for the catch; should add to array first then increment >.<