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 Jurassic · Feb 24, 2014 at 03:37 AM · nullreferenceexceptionarrays

Array assignments not behaving as expected.

I am getting the warning "savedMat is never assigned to and will always have its default value null" when building this script. I am definitely assigning to it in the Start function, however the warning appears to actually be correct because later in the script I get null reference exceptions. What am I doing wrong?

     private GameObject[] findRobots;
     private Material[] savedMat;
 
     public Material eyeMaterial;
 
     // Use this for initialization
     void Start () {
 
         findRobots = GameObject.FindGameObjectsWithTag("GuardBot");
         
         if(findRobots.Length==0 || findRobots == null){
             Debug.Log ("No objects tagged as \"GuardBot\", RobotEyes will not work.");
             return;
         }
 
         for(int n=findRobots.Length; n<findRobots.Length; n++){
             savedMat[n] = findRobots[n].renderer.material;
         }
     }
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 Kiloblargh · Feb 24, 2014 at 03:41 AM

Before assigning anything to a slot in an array, you have to initialize the array with new and the size:

 savedMat = new Material [findRobot.Length];
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 rutter · Feb 24, 2014 at 03:41 AM

Seems like you need something closer to this:

 savedMat = new Material[findRobots.Length];
 for(int n=0; n<findRobots.Length; n++){
     savedMat[n] = findRobots[n].renderer.material;
 }

You need to create the array before you can populate 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 Jurassic · Feb 24, 2014 at 03:53 AM 0
Share

EDIT: Never$$anonymous$$d, figured it out! Simple mistake on my part, the for loop declaration only filled the last object.

Thanks! That fixed them being empty, but now the actual populate script isn't running. It no longer throws an error, but the material that is being saved in saved$$anonymous$$at[n] is empty. (Shows up as pink in game) The materials of the gameObjects in findRobots are definitely not empty. Any idea what's wrong with the for loop?

I ran it with a Debug.Log in the loop and it never gets called.

avatar image Kiloblargh · Feb 24, 2014 at 04:22 AM 0
Share

Because you are starting it at the end. If n starts out as findRobots.Length and increments, it will never be less than`findRobots.Length.` The for loop should start with 0.

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

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

Related Questions

Null reference when accessing GameObject in the Array(C#) 1 Answer

Multidimensional GameObject Arrays 1 Answer

NullReference when accessing GameObject in array (C#) 1 Answer

Split() into a fixed length array? 1 Answer

Access System.Serializable class from script? 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