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 radwan92 · Sep 27, 2013 at 06:12 PM · c#arraypassingnull referance exeption

How to pass objects through array?

So here is how the code looks like:

 SomeClass someObject1;
 GameObject gamObjX;
 
 SomeClass[] myArray;
 
 void Start ()
 {
    myArray = new SomeClass[1];
    SomeMethod(ref gamObjX);
 }

 void SetElements ()
 {
    myArray[0] = someObject1;
 }

 void SomeMethod (ref GameObject gamObj)
 {
    myArray[0] = gamObj.GetComponent<SomeClass>();   // SomeClass script is attached to gamObj
 }
 
 void Update ()
 {
    someObject1.DoSomethingWithGamObj()   // Error: Object reference not set to an instance of an object
    myArray[0].DoSomethingWithGamObj()   // Something is done
 }

The only way I know how to solve this is using pointers. But Unity doesn't allow pointers so... Why does it happen? Are the elements in the array copies of actual variables? What happens when I call SetElements() method? I would like myArray[0] to be someObject1 (or 'reference of' should I say?) so if I do myArray[0].someVar = 1; print(someObject1.someVar); would print 1 instead of null or some basic value.

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

Answer by perchik · Sep 27, 2013 at 06:19 PM

A few things:

First, it's not that Unity does't allow pointers, its that C# doesn't have pointers. [If you think you're writing Javascript(UnityScript) you're doing it way wrong]

Second, since gamObj is global, you don't have to pass it by reference (or pass it in at all)

Now, to answer your question, I think you're confused at how things work.

Just because you set myArray[0] = someObject1, doesn't mean that someObject1 will always equal myArray[0].

You're correct in thinking if you had used pointers, this might work. Anyway, to fix it:

After you do myArray[0] = gamObj.GetComponent(); // SomeClass script is attached to gamObj

just do someObject1 = myArray[0];// or gameObj.GetComponent(); to actually assign the component to someObject1

Now. After that's all said, I'm not sure why you want two variables that reference the same component.

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 radwan92 · Sep 27, 2013 at 06:25 PM 1
Share

Works like a charm, thank you very much. Is there any way to skip the assigning step, so by operating on myArray[0] I would operate on someObject1 at the same time? I want it so because array can be used in loops, and standalone variable gives me clear view at whats happening in the code.

avatar image perchik · Sep 27, 2013 at 06:54 PM 0
Share

If my answer solved your original question, please mark it as the answer.

Now, with that said, I'd say if you really really want that functionality, switch to using a dictionary with the [string] name of the object as the key, and the component as the value. Then you could loop over it using the dictionary, or access specific items clearly with the dictionary... something like this:

 using System.Collections.Generic;
 
 Dictionary<string, SomeClass> myDict;
 
 void Start(){
    myDict = new Dictionary<string, SomeClass>();
    myDict.Add("someObject1", gamObj.GetComponent<SomeClass>());
    //or
    myDict["someObject1"] = gamObj.GetComponent<SomeClass>();
 }
 
 void heresHowToLoopOver(){
    
    foreach( $$anonymous$$eyValuePair<string, SomeClass> pair in $$anonymous$$yDict){
        Debug.Log("Doing something with object: " + pair.$$anonymous$$ey); 
        pair.Value.DoSomethingWithGamObj();
    }
 
    //or loop over the keys:
    foreach( string key in $$anonymous$$yDict.$$anonymous$$eys){
        Debug.Log("Doing something with object: " + key); 
       myDict[key].DoSomethingWithGamObj();
    }
 }
 
 
 void heresHowToAccess(){
    myDict["someObject1").DoSomethingWithGamObj();/// there's your code clarity.
 
 }

Btw, this isn't guaranteed to compile, I just wrote it on the fly

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

Why is passing this array causing some of its values to become null? 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to properly create a 2 dimensional array of an object. [C#] 1 Answer

Find Closest Object by Name 0 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