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 irdc · Jun 24, 2013 at 04:38 AM · arrayobjectdynamic

Cannot access class array in dynamically created object

Using .js:

In a random script I create a dynamic object (empty GameObject):

 var object = new GameObject("Hi");

Attach a component

 object.AddComponent("Blah");

Blah.js looks as follows:

 class Whatever {
    var name : String;
 }

 var Nifty : Whatever[] = new Whatever[15];

 function DoSomething(){
    this.Nifty[0].name = "Sparky"; 
 }

 function Start(){
    this.DoSomething();
 }

This causes:

NullReferenceException: Object reference not set to an instance of an object

The error is occurring when I try and access this.Nifty[0]; I can't change or access any of the variables

Now I change it:

 function Update(){ 
    this.DoSomething(); 
 }

The same error occurs. But now, if I click on the dynamically created object ("Hi") in the Hierarchy, the error stops in the Console AND this.Nifty[0].name is changed to Sparky. So, there isn't really an error in syntax here.

This bug did not occur when I used non-dynamically created GameObject and simply attached the script to a empty object already in the scene.

What is going on here?

Thank you.

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

Answer by whydoidoit · Jun 24, 2013 at 04:50 AM

Attaching a component by script does not set any of the "default" parameters that you see on the script when it is selected in the Inspector (I mean when the script is selected in the project, not an instance selected in the hierarchy). This is why most people just leave those blank and programmatically set them. Adding a component in the editor does set these values.

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
avatar image
0

Answer by irdc · Jun 24, 2013 at 04:59 AM

Well, get to answer my own question after someone on the forum responded. Hooray!

Basically, when attaching the script to a non-dynamic object, it is possible to just use the following:

 var Nifty : Whatever[] = new Whatever[15];

or

 var Nifty = new Whatever[11];

This was causing some problems with dynamic objects. With dynamic objects, I had to use the following:

 function Start () {
    for(var i=0;i<11;i++) {
       this.Nifty [i] = new Whatever();
    }
    this.DoSomething();
 }

That worked!

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

16 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

Related Questions

How to preserve array of custom objects in inspector? 0 Answers

Add instantiate object to GameObject[] 2 Answers

What are GameTiles? 1 Answer

Unity Object Array C# 2 Answers

Swapping elements of an array 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