x


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.

more ▼

asked May 14 '10 at 02:50 AM

xToxicInferno gravatar image

xToxicInferno
485 24 28 41

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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:

Which kind of Array or Collection should I use?

more ▼

answered May 14 '10 at 07:21 AM

duck gravatar image

duck ♦♦
41k 92 148 415

Upvoted - is there a Duck fanclub that I can join?

Oct 01 '10 at 03:24 AM Marowi

@Marowi 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

Dec 12 '10 at 09:00 PM Cawas
(comments are locked)
10|3000 characters needed characters left

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++;
   }
}
more ▼

answered May 14 '10 at 07:42 AM

Extrakun gravatar image

Extrakun
1.3k 44 56 70

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!

May 14 '10 at 08:21 AM duck ♦♦

Thanks for the catch; should add to array first then increment >.<

May 14 '10 at 04:46 PM Extrakun
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x355

asked: May 14 '10 at 02:50 AM

Seen: 7643 times

Last Updated: May 14 '10 at 02:50 AM