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 Ochreous · Oct 01, 2012 at 03:23 AM · error

Null Reference Exception Unless Scripted Object is Selected

Hi everyone, I have found an odd little glitch in one of my scripts.For some reason when I startup my script unity keeps giving me a null reference exception unless I select the object that the script is attached to.Why is it doing this and how do I fix this?

Comment
Add comment · Show 3
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 karl_ · Oct 01, 2012 at 04:35 AM 0
Share

Can you post the relevant code? It's a bit difficult to diagnose without it (as an example, do you make use of the OnEnable function?).

avatar image wrstscrnnm6 · Oct 01, 2012 at 04:36 AM 0
Share

Can you post the script?

avatar image Ochreous · Oct 01, 2012 at 04:43 AM 0
Share

I don't know why but it keeps saying that the BoolList[i].BoolValue = true; is causing the null reference.

[System.Serializable]

public class BoolValueClass

{

public bool BoolValue = false;

}

public List BoolList;

Void OnGUI(){

for (int i = 0; i < BoolList.Count; i++)

             if (GUILayout.Button("BoolButton", 

GUILayout.Width(75), GUILayout.Height(25)))

             {

             BoolList[i].BoolValue = true;

             } 

}

Also the BoolList is suppose to inherit from BoolValueClass for some reason I can't copy/paste that bit in.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by karl_ · Oct 01, 2012 at 04:52 AM

Based on the script, it looks like `BoolList` is not initialized, and therefore null. To avoid this, initialize BoolList in your declaration.

 public List<bool> BoolList = new List<bool>();

Also, I'm not really sure what your script is trying to accomplish here. It looks like it's missing parts, but I'm assuming BoolValueClass is some sort of container you wish to create a list of. In that case, the syntax would be

 public List<BoolValueClass> BoolList = new List<BoolValueClass>();

Edit: Full script showing how to work with a data class like this:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class BoolValueClass
 {
     public bool BoolValue = false;
 
     public BoolValueClass(bool val)
     {
         BoolValue = val;
     }
 }
 
 public class BoolListExample : MonoBehaviour {
 
     public List<BoolValueClass> BoolList = new List<BoolValueClass>();
 
     void Awake()
     {
         // fill the boollist with some values
         for(int i = 0; i < 10; i++)
         {
             if(i % 2 == 0)
                 BoolList.Add(new BoolValueClass(true));
             else
                 BoolList.Add(new BoolValueClass(false));
         }
     }
 
     void OnGUI()
     {
         for (int i = 0; i < BoolList.Count; i++)
         {
             if (GUILayout.Button("BoolButton", GUILayout.Width(75), GUILayout.Height(25)))
                 BoolList[i].BoolValue = true; 
         }
     }
 }
Comment
Add comment · Show 6 · 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 Ochreous · Oct 01, 2012 at 04:58 AM 0
Share

I"m still getting a null reference exception.

avatar image karl_ · Oct 01, 2012 at 05:01 AM 0
Share

Try adding a null check before your for loop: `if(BoolList != null) for ...`

avatar image Ochreous · Oct 01, 2012 at 05:05 AM 0
Share

Nope, still getting a null reference exception. I've noticed that it only happens with bools. If I made BoolValue an int it would work fine.

avatar image karl_ · Oct 01, 2012 at 05:27 AM 0
Share

I've updated my answer with a fully working script that demonstrates what I think you're attempting to do.

avatar image Ochreous · Oct 01, 2012 at 05:45 AM 0
Share

I tested your script and I didn't receive any null reference exceptions. Could you please tell me what you did in your script that fixed the null references?

Show more comments

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

11 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Need help calling a script to another keep getting errors 1 Answer

how to change content offset of a style 1 Answer

Tracking Down GUI Errors 0 Answers

2D toolkit scripting error - Very Basic Stuff 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