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 vagos21 · Jan 08, 2013 at 10:55 AM · arrayobjectaddbuiltin

adding object to builtin array works in one script, not in another?

Hello,

I've been using the builtin javascript array to keep together some gameobjects loading on startup.

while this is working:

 var PiecesLibrary : GameObject[];
 .
 .
 .
 PiecesLibrary += [pieceGO];

in another script i'm doing exactly the same:

 var connectorsArr : GameObject[];
 .
 .
 .
 function AddConnectorToList(){
     connectorsArr += [connectorToAdd];
 }

i get "object reference not set to an instance of an object" when calling the function on the 2nd example! connectorToAdd is a valid gameObject just like pieceGO, the only difference is i call this function from another script. this error is like there is no "initialisation" of the connectorsArr? but it works on the first script...

please don't tell me to change built in arrays to lists the code is already quite complete and since it works with the first script i'd wish to see it work in the second one too....

thank you for your time!

EDIT:

ok, i will explain a bit better:

i build something like a lego assembly so i load some parts. for each part i have connection points as gameObjects (which i use later to make joints) which i want to keep track in an array (and not hold them as children of the piece) . so i thought i'd attach a script to each piece (called "Pieces") and keep that array in there. So i have the pieces script that i attach to each of the pieces when it's created:

         //CREATE PIECE
         var pieceGO:GameObject = new GameObject();
         var filter:MeshFilter = pieceGO.AddComponent(MeshFilter);
         var renderer:MeshRenderer = pieceGO.AddComponent(MeshRenderer);
         var collide:MeshCollider = pieceGO.AddComponent(MeshCollider);
         filter.mesh = Resources.Load(piecesDirectory+pieceName,Mesh);
         collide.sharedMesh =Resources.Load(collidersDirectory+pieceName,Mesh);
         pieceGO.collider.isTrigger=true;
         pieceGO.name=pieceName;
         //ATTACH PIECES SCRIPT
         pieceGO.AddComponent("Pieces");
         var pieceScript = pieceGO.GetComponent("Pieces") as Pieces;
             pieceScript.AddConnectorToList(connGO);

and this is the "pieces" script:

 var Selected : boolean = false;
 var highlighted : boolean = false;
 var groupName : String = "";
 var connectorsArr : GameObject[];
 
 private var selectedMaterial:Material;
 
 function Start(){
 }
 
 function Update(){
     if (Selected){
         gameObject.renderer.material = selectedMaterial;
     }
 }
 
 function AddConnectorToList(connGO:GameObject){
     connectorsArr += [connGO];
 }

all lines of the code work and load objects and everything works perfectly while this line connectorsArr += [connGO];

returns an error: object reference not set to an instance of an object

Any new ideas?

Comment
Add comment · Show 8
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 DanielJF · Jan 08, 2013 at 11:50 AM 0
Share

Need a lil more information to help on this script could you show an example where you call the function ?

avatar image vagos21 · Jan 08, 2013 at 11:58 AM 0
Share

ok i have come down to this: i attach the script that has the problem dynamically to a gameObject. the script is called "Pieces" and it has a Start function inside it. i do some test print at the "Start" function but i get nothing printed. so this means that when i attach the script to the gameObject the script is not "initialised" or something? so every variable in that script is not yet accessible? how would i "initialise" the script so that its code is run and i get my connectorsArr : GameObject[] initialised?

avatar image DanielJF · Jan 08, 2013 at 12:07 PM 0
Share

I don't realy get what you mean but i can show you how to acces scripts from other scripts if that is the problem :

 //1st script
 class testfile extends $$anonymous$$onoBehaviour
 {
 static var main : testfile;
 var random : int = 0;
 
 function Awake()
 {
     main = this;
 }
 }
 
 //2nd script
 class testfile2 extends $$anonymous$$onoBehaviour
 {
 
 function start()
 {
 Debug.Log(testfile.main.random)//0;
 }
avatar image DanielJF · Jan 08, 2013 at 12:08 PM 0
Share

I see it's pretty anoying to read like this so i hope you can read it else i will do a repost in answer

avatar image DanielJF · Jan 08, 2013 at 12:29 PM 0
Share

Awesome thx for the hint

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by vagos21 · Jan 08, 2013 at 04:33 PM

the problem was really this like i sensed:

When attaching a script to a gameObject dynamically, the array values are not initialised so when trying to change them there is an error, unline the editor which i guess initialises them automatically.

so all i had to do is add a line to initialise it in my code

 connectorsArr = new GameObject[0];

and then happily add stuff to it directly

 connectorsArr += [connGO];

i hope this helps others!!

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

9 People are following this question.

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

Related Questions

.Add not a function of my array. 1 Answer

Problems adding to a Builtin Array in C# 1 Answer

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

Adding to an array - C# 2 Answers

Unity networking tutorial? 6 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