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 CoalCzar · Sep 13, 2013 at 02:57 PM · instantiatearraymaterialnullreferenceexceptionobject reference

Material[] Object reference not set when instantiating

I'm trying to fill an array of Materials with the materials from an array of GameObjects.

Right now, I have confirmed that GameObject array is filled with the correct gameobjects. I have also confirmed that it gets inside the for loop, but it stops right when I start to instantiate the Material array.

 private Material[] aryOriginalMaterial;
 private GameObject[] aryLukewarmGO;

 aryLukewarmGO =  GameObject.FindGameObjectsWithTag("Lukewarm");
 for (int i = 0; i < aryLukewarmGO.Length; i++)
 {
     //Gets here, but stops at next line (next line is line 91 in the error)
     aryOriginalMaterial[i] = aryLukewarmGO[i].transform.renderer.material;
 }

In Unity, I get the error:

 NullReferenceException: Object reference not set to an instance of an object
 (wrapper stelemref) object:stelemref (object,intptr,object)
 CharacterInput.GoInfrared () (at Assets/Scripts/Player/CharacterInput.cs:91)
 CharacterInput.Update () (at Assets/Scripts/Player/CharacterInput.cs:42)
Comment
Add comment · Show 1
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 vexe · Sep 13, 2013 at 03:00 PM 0
Share

Did you debug? - If you didn't, please do and see what's being null (break right before that line gets executed). Either the renderer or the material is being null. $$anonymous$$ake sure there's a renderer attached to your gameObject, with a material. Also make sure your arrays are assigned correctly. Are you assigning them somewhere from within your code? If not are you assigning them from the inspector? If not, please do.

2 Replies

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

Answer by fafase · Sep 13, 2013 at 03:03 PM

You need to set the size of the other array.

 private Material[] aryOriginalMaterial;
 private GameObject[] aryLukewarmGO;

This creates two reference variables to the type they are, like pointers. But they contain nothing.

 aryLukewarmGO =  GameObject.FindGameObjectsWithTag("Lukewarm");

The method returns an array, it actually creates it and gives the address to the array reference. So you have your object.

but when you do :

 aryOriginalMaterial[i] = aryLukewarmGO[i].transform.renderer.material;

The one on the left has no storage location, it is just a null pointer/reference.

So either you need to create the array:

  private Material[] aryOriginalMaterial = new Material[size];

But you need to know how big it should be or you use a list:

 List <Material> materialList = new List<Material>();
Comment
Add comment · Show 1 · 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 CoalCzar · Sep 13, 2013 at 03:41 PM 0
Share

Perfect. I think because the script to fill the GameObject array created it and filled it, I didn't realize I needed to create the array as a separate step.

I added:

 aryOriginal$$anonymous$$aterial = new $$anonymous$$aterial[aryLukewarmGO.Length];

before the for loop and it worked like a charm.

avatar image
0

Answer by lvictorino · Sep 13, 2013 at 03:02 PM

First, the error tells you that you may want to check if aryOriginalMaterial[i] and aryLukewarmGO[i] exists. Then the problem occurs because your Material array has not been initialized. You may want to use the "new" keyword to instantiate it.

I hope it helps,

Cheers.

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

17 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

Related Questions

How to Instantiate an Integer Array in an Array of Arrays? 1 Answer

Instantiating gameobjects in an array / class problem | NullReferenceException: 0 Answers

NullReferenceException with an Array 2 Answers

IndexOutOfRangeException: Array index is out of range when using an Array and instantiating 2 Answers

Access Multiple Renderer Materials? 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