Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by xToxicInferno · May 14, 2010 at 02:50 AM · arrayarrays

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.

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
4

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:

Which kind of Array or Collection should I use?

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 · Oct 01, 2010 at 03:24 AM 1
Share

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

avatar image cregox · Dec 12, 2010 at 09:00 PM 0
Share

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

avatar image ocimum · Jul 29, 2015 at 11:40 AM 0
Share

link is broken

avatar image
2

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++;

} }

Comment
Add comment · Show 2 · 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 duck ♦♦ · May 14, 2010 at 08:21 AM 0
Share

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!

avatar image Extrakun · May 14, 2010 at 04:46 PM 1
Share

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

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

No one has followed this question yet.

Related Questions

my QandA array is not working when you choose 3 wrong answers 1 Answer

Array empties values on RunTime? 1 Answer

Array with two values per element 1 Answer

Having Multiple Controllable FPS Units Selected From A Singular RTS Mode? 0 Answers

Array Within Index 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