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 e-bonneville · Feb 14, 2011 at 11:36 PM · guiarraynullreferenceexception

NullReferenceException in an array

NullReferenceException has brought me to my knees once more in the following script:

using UnityEngine; using System.Collections;

public class ColorPicker : MonoBehaviour {

 private Texture2D[] currentColors;
 private GameObject[] currentCubes;
 Texture2D currentColorTexture;

 void Start() {
     currentColorTexture= new Texture2D(1,1);
 }

 void OnGUI() {
     for (int aY = 0;aY<currentColors.Length-1;aY++) {

         currentColorTexture = currentColors[aY];
         GUIStyle currentColorStyle = new GUIStyle();
         currentColorStyle.normal.background = currentColorTexture;

         if(GUI.Button(new Rect(10,(aY*10) + 10, 10,50), currentColors[aY], ""))
             Debug.Log("ah");


     }

 }

 public void UpdateColors() {
     currentCubes = GameObject.FindGameObjectsWithTag("Cube");       
     currentColors = new Texture2D[currentCubes.Length];

     for(int aA = 0;aA<currentColors.Length;aA++) {
         currentColors[aA] = new Texture2D(1,1);
         currentColors[aA].SetPixel((int)0,(int)0, currentCubes[aA].renderer.material.color);
     }

     for (int aB = 0;aB<currentCubes.Length;aB++) {          
         for (int aC = 0;aC<currentColors.Length;aC++) {
             currentColorTexture.SetPixel(0,0, currentCubes[aB].renderer.material.color);
             currentColors[aB] = currentColorTexture;
         }
     }

 }

}

I've literally spent six and half hours trying to find the error in this simple piece of work but it keeps giving me the high-blood-pressure-inducing NullReferenceException.

I would deeply appreciate it if somebody could explain what I'm doing wrong here.

Thanks, Elliot Bonneville

Comment
Add comment · Show 2
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 · Feb 14, 2011 at 11:47 PM 1
Share

In the OnGui() ForLoop, your end condition is '`array.Length-1`', but in the UpdateColors() ForLoop, your end condition is array.Length ... is this intentional?

avatar image e-bonneville · Feb 14, 2011 at 11:53 PM 0
Share

No, but it shouldn't cause a problem. I was in the process of deter$$anonymous$$ing which one worked, and forgot to change the OnGUI loop. Thanks for pointing that out, though.

1 Reply

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

Answer by tertle · Feb 14, 2011 at 11:46 PM

At a first glance.

currentCubes = GameObject.FindGameObjectsWithTag("Cube");       
currentColors = new Texture2D[currentCubes.Length];

Could potentially cause a NullReferenceException if nothing with a Cube tag exists.

currentCubes = GameObject.FindGameObjectsWithTag("Cube");
if (currentCubes != null)
{
    currentColors = new Texture2D[currentCubes.Length];
    ...
}

Should fix that.

Not sure if that's your specific issue though.

-edit-

also

void OnGUI() {
    for (int aY = 0;aY<currentColors.Length-1;aY++) {

If it runs before

public void UpdateColors()

Which I'm not sure when you call, will cause a NullReferenceException.

Should initialize it to 0.

private Texture2D[] currentColors = new Texture2D[0];

Or check if it's null before the for loop. Or call updatecolours from start() etc.

Comment
Add comment · Show 4 · 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 e-bonneville · Feb 14, 2011 at 11:47 PM 0
Share

Nope, I most definitely have objects tagged Cube in my scene. Thanks for replying, though. :)

avatar image tertle · Feb 14, 2011 at 11:52 PM 0
Share

Check edited post.

avatar image · Feb 14, 2011 at 11:53 PM 2
Share

I posted the same as your updated answer in a comment on the question, but have deleted it - upvote ins$$anonymous$$d!

avatar image e-bonneville · Feb 15, 2011 at 12:01 AM 0
Share

Calling UpdateColors() from Start seems to have partially solved the problem. Thanks!

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

No one has followed this question yet.

Related Questions

How to show an array of custom objects in Inspector? 2 Answers

Unity Object Array C# 2 Answers

Setting parent gives error? 2 Answers

Pushing textures in an Array 2 Answers

Problem with Singleton and NullReferenceException? 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