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 Silx · Jan 08, 2012 at 03:01 AM · javascriptgameobjectarray

Array problem

I get null reference errors when getting or setting any variable in this array. I've tried both methods of setting the array size initially (as it is now).. and reconstructing the area with the commented line in function Awake() that you see below. What am I doing wrong?

Also if anyone knows a more elegant way of looping through all the objects than what I'm doing now (a simple counter... ) I would be very grateful. I suppose I could use a generic for loop..

     class ObjectData
     {
         var x : float;
         var y : float;
         var z : float;
         var name : String;
     }
     
      class MapData
      {
        var _iLevel : ObjectData[];
        function MapData() { }
     }
 
     private var myData : MapData;
 
 function Awake () { 

         myData=new MapData();
 
         var gameObjects : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
                   myData._iLevel = new ObjectData[gameObjects.Length];
         
                   for (var stats : GameObject in gameObjects) {
                     myData._iLevel[count].x = stats.transform.position.x;
                    myData._iLevel[count].y = stats.transform.position.y;
                      myData._iLevel[count].z = stats.transform.position.z;
                       myData._iLevel[count].name = stats.name;
                      count++;
                   }    
 
 }
Comment
Add comment · Show 3
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 syclamoth · Jan 08, 2012 at 03:05 AM 0
Share

Could you show us the 'UserData' object? You haven't posted that part of the script! In any case, it seems that there's no real relation between the number of elements in 'gameObjects' and the number of elements in 'myData._iLevel'. Why have you commented out the line just before the for-loop begins? It seems that that line is what's keeping the two values the same!

avatar image Silx · Jan 08, 2012 at 03:15 AM 0
Share

Fixed the variable type. It should be a $$anonymous$$apData type. Regarding the commented out line, I didn't think it needed to be there because the array was initialized with a static size in the class definition. However, now I've changed it back to how I want it to work. Define the array with no size, then recreate it using the number of GameObjects that I find. Still no go with this.

avatar image syclamoth · Jan 08, 2012 at 03:42 AM 0
Share

Well, I'm afraid I can't see any more problems here! Try putting Debug.Logs in everywhere, so that you can have some idea of exactly what is the null reference, and where the problems start occurring.

1 Reply

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

Answer by Michael Schenck · Jan 08, 2012 at 04:16 AM

You need to create the ObjectData instances - inside the for loop at top do myData.ILevel[count] = new ObjectData(); You just have an array of references to object data that is null. In C# I think you could make this a struct and you would be ok, but it is a class here, so you need to allocate it.

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 syclamoth · Jan 08, 2012 at 04:54 AM 0
Share

Yes, that'd be it.

avatar image Silx · Jan 08, 2012 at 05:33 AM 0
Share

That did it. Thanks $$anonymous$$ike!

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

6 People are following this question.

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

Related Questions

Activate/Deactivate Script from another Object. 3 Answers

Problem with moving objects in array? 2 Answers

How to store aspects (position, name, tag) of a game object in an array to be used for reinstantiation? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Quest Script Help 2 Answers


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