Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 karl_ · Mar 13, 2011 at 09:06 PM · gameobjectontriggerbuiltin array

Create an array of gameobjects in trigger?

I'm trying to create an array and keep it updated based on what objects are currently in a trigger. Here is what I have so far, the problem is that I am getting a null reference error when sending objEntering.gameObject to the augmentArray function.

function OnTriggerEnter(objectEntering : Collider) { objectsInTrigger++; augmentArray(objectEntering.gameObject, fishInRange); Debug.Log("Object name : " + objectEntering.gameObject); Debug.Log("Obj in trigger " + objectsInTrigger);

}

function augmentArray(objToAdd : GameObject , array : GameObject[]) { var tempArray = new GameObject[array.length+1];

 for(i = 0; i < array.length; i++)
     tempArray[i] = array[i];

 tempArray[array.length+1] = objToAdd;

     // Refill the original array etc etc

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Statement · Mar 13, 2011 at 09:13 PM

I don't know any null reference problems (unless your array is null to begin with) but I see you're indexing out of range here:

tempArray[array.length+1] = objToAdd;

Did you mean:

tempArray[array.length] = objToAdd;

You might want to use a List.<GameObject> instead, it supports adding new items to it so theres no need creating copies of arrays.

import System.Collections.Generic;

// I am no JS guru so you might be able to strip off the new keyword also. var fishInRange : List.<GameObject> = new List.<GameObject>();

function OnTriggerEnter(objectEntering : Collider) {
objectsInTrigger++; // OR you could use fishInRange.Count fishInRange.Add(objectEntering.gameObject); Debug.Log("Object name : " + objectEntering.gameObject); Debug.Log("Obj in trigger " + objectsInTrigger); }

Comment
Add comment · Show 5 · 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 karl_ · Mar 13, 2011 at 09:32 PM 0
Share

I'd been intentionally avoiding list and js arrays due to performance issues, along with the issue of (in my experience) spotty performance on the iPhone/iPad. However, you were correct in the index out of range error. oops. It's working as intended now, thanks.

avatar image Statement · Mar 13, 2011 at 10:18 PM 0
Share

I don't know about performance issues on iPhone/iPad, to me it just sounds a bit odd, however the access times aren't that much of a deal. See this topic about it, it's quite interesting: http://answers.unity3d.com/questions/32048/is-listt-as-fast-to-access-as-a-standard-array/32068#32068 however, allocating a new array each time you want to add something surely must be one of the most inefficient ways to go about.

avatar image karl_ · Mar 13, 2011 at 10:21 PM 0
Share

That was a very helpful post indeed. I think I may just scrap this whole bit and utilize a list.

avatar image Eric5h5 · Mar 13, 2011 at 10:30 PM 2
Share

@karl: there are no performance issues with List. Avoid JS Array, yes.

avatar image Niklas · Mar 13, 2011 at 11:29 PM 2
Share

If you are concerned about performance you can try to avoid dynamic typing also. Read more here: http://unity3d.com/support/documentation/ScriptReference/index.Performance_Optimization.html

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

Clearing Dynamically GameObject Array 1 Answer

Move object when player reach trigger 2 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Gameobject Inspector display is ... different 3 Answers

Destroy gameobject or make invisible and recycle? 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