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
0
Question by TdragonR1386 · Mar 17, 2014 at 02:38 AM · collisioncolliderarray

How to I add a collision's GameObject to an array?

The code with all the other bits taken out:

var lGround:Array;

function Start () {

var lGround= new Array();

}

function OnCollisionEnter(other:Collision){

 if(other.gameObject.tag == "Ground"){

     var gcontainer:GameObject=other.collider.gameObject;

     print(gcontainer);

     lGround.Add(gcontainer);

 }

}

The print line returns (5x): Cube (UnityEngine.GameObject) UnityEngine.MonoBehaviour:print(Object)

Which seems to be the cube object. (There are five of them that it is falling into.) I then get this error (5x): NullReferenceException: Object reference not set to an instance of an object Lemming.OnCollisionEnter (UnityEngine.Collision other) (at Assets/Scripts/Lemming.js:46)

How do I add the object colliding to an array properly?

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
0
Best Answer

Answer by ransomink · Mar 17, 2014 at 12:04 PM

You are declaring the var lGround twice: (1) at the top of your script, (2) inside the Start() function. Anything you declare inside of a function will exist only inside the scope of 'that' function. Because of this, you cannot access var lGround = new Array(); outside of the Start() function.

However, you did declare lGround at the top of your script, but, you never created an instance for it, like, lGround = Array();. Inside of your Start() function simply delete "var"

 // INSPECTOR VARIABLES //
 public var lGroundArray : GameObject[];// a built-in Array (will display in the editor)
 
 //PRIVATE VARIABLES //
 private var lGround : Array;// an array holding all ground collision GameObjects
 
 // used for initialization
 function Start ()
 {
     lGround = Array ();// create an instance of the array
     Debug.Log ( "lGround Array: " + lGround );// display the array (for reference; it should be empty, of course :P)
 }
 
 // check collision for rigidbody GameObjects
 function OnCollisionEnter ( other : Collision )
 {
     // check if the collided GameObject has the specified tag
     if ( other.gameObject.tag == "Ground" )
     {
         var gContainer : GameObject = other.gameObject;
         Debug.Log ( "gContainer: " + gContainer.name );// display the collided GameObject
         lGround.Push ( gContainer );// add the collided GameObject to the array (which is no longer empty)
         
         Debug.Log ( "lGround Array: " + lGround );// display the contents of the array (updated with the collided GameObject)
         
         /* copy the Unity javascript array into the built-in array.
         This will display the contents of the lGround array (your collided GameObjects)
         inside the editor AND update it everytime a new collision occurs, so long as the tag matches */
         lGroundArray = lGround.ToBuiltin ( GameObject ) as GameObject[];
     }
 }
Comment
Add comment · Show 4 · 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 TdragonR1386 · Mar 18, 2014 at 09:54 PM 0
Share

This worked fantastically. Thank you!

avatar image whydoidoit · Mar 19, 2014 at 02:16 AM 0
Share

Really, don't use Arrays they are slow and not typesafe. Use Lists.

avatar image ransomink · Mar 20, 2014 at 04:03 AM 0
Share

Can you do Lists in Unityscript, I'm not sure if that's an option? If so, please, do tell. Anything to save on speed...

avatar image whydoidoit · Mar 20, 2014 at 04:05 AM 0
Share

Yes, my answer below shows the method.

avatar image
0

Answer by whydoidoit · Mar 17, 2014 at 10:03 AM

First you should really

     import System.Collections.Generic;

And use

   var lGround = new List.<GameObject>();

They're faster and typesafe (you will also get intellisense on the members).

Next that line needs to appear outside the Start function because at the moment it is just a local variable that exists in Start! You are not initializing the one in the script (because you added var to the beginning of the initialization line you created a local variable with the same name!)

Comment
Add comment · 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

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detect multiple collisions and assigning them to an array? 0 Answers

Detecting if object in array collides with another object in same array 1 Answer

setting a transform var to an object that collided 1 Answer

Ignore Colliders of an Object 1 Answer

Object Collision PLEASE HELP!!! 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