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 NutellaDaddy · May 04, 2014 at 09:39 PM · c#functionnull reference exceptioncalling

NullReferenceException: Object reference not set to an instance of an object

When I try to call this function from another script that I have everything is fine until I test the game and it says the above error. Here's the full thing.NullReferenceException: Object reference not set to an instance of an object BuildingMenu.OnGUI () (at Assets/Scripts/BuildingMenu.cs:53) Can anyone tell me what the problem is?

 using UnityEngine;
 using System.Collections;
 
 public class BuildingMenu : MonoBehaviour 
 {
     public GameObject[] cockpit;
     public GameObject[] control;
     public GameObject[] propulsion;
     public GameObject[] fuelContainers;
     public GameObject[] aerodynamics;
     public GameObject[] structural;
     public GameObject[] utilities;
     public GameObject[] research;
 
     public Texture2D partSelectionBackground;
     public GUIStyle partSlotButton;
     public GUIStyle tabButton;
 
     private bool cockpitWindowOpen;
     private bool controlWindowOpen;
     private bool propulsionWindowOpen;
     private bool fuelContainersWindowOpen;
     private bool aerodynamicsWindowOpen;
     private bool structuralWindowOpen;
     private bool utilitiesWindowOpen;
     private bool researchWindowOpen;
 
     public static int cockpitAmounts;
     private int cols = 3;
     public int toolBarInt;
     private PlacingParts placingparts;
 
     void Start () 
     {
         cockpitAmounts = 0;
         toolBarInt = 1;
         placingparts = GetComponent<PlacingParts> ();
     }
     void Update () 
     {
         ToolBarInt ();
     }
     void OnGUI()
     {
         //THE WINDOW FOR THE COCKPITS
         if(cockpitWindowOpen == true)
         {
             GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
                 for (int i = 0; i< cockpit.Length; i++)
                     {
                         if(GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),cockpit[i].name, partSlotButton))
                         {
                             placingparts.PlaceItem(cockpit[i]);
                         }
                     }
             GUI.EndGroup ();
         }
         //THE WINDOW FOR THE Control
         if(controlWindowOpen == true)
         {
             GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
             for (int i = 0; i< control.Length; i++)
             {
                 GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),control[i].name, partSlotButton);
             }
             GUI.EndGroup ();
         }
         //THE WINDOW FOR THE PROPULSION
         if(propulsionWindowOpen == true)
         {
             GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
                 for (int i = 0; i< propulsion.Length; i++)
                 {
                 GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),propulsion[i].name, partSlotButton);
                 }
             GUI.EndGroup ();
         }
         //THE WINDOW FOR THE PROPULSION
         if(fuelContainersWindowOpen == true)
         {
             GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
             for (int i = 0; i< fuelContainers.Length; i++)
             {
                 GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),fuelContainers[i].name, partSlotButton);
             }
             GUI.EndGroup ();
         }
         //THE WINDOW FOR THE AERODYNAMICS
         if(aerodynamicsWindowOpen == true)
         {
             GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
             for (int i = 0; i< aerodynamics.Length; i++)
             {
                 GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),aerodynamics[i].name, partSlotButton);
             }
             GUI.EndGroup ();
         }
         //THE WINDOW FOR THE STRUCTURAL
         if(structuralWindowOpen == true)
         {
             GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
             for (int i = 0; i< structural.Length; i++)
             {
                 GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),structural[i].name, partSlotButton);
             }
             GUI.EndGroup ();
         }
         //THE WINDOW FOR THE UTILITIES
         if(utilitiesWindowOpen == true)
         {
             GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
             for (int i = 0; i< utilities.Length; i++)
             {
                 GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),utilities[i].name, partSlotButton);
             }
             GUI.EndGroup ();
         }
         //THE WINDOW FOR THE RESEARCH
         if(researchWindowOpen == true)
         {
             GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
             for (int i = 0; i< research.Length; i++)
             {
                 GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),research[i].name, partSlotButton);
             }
             GUI.EndGroup ();
         }
         WindowTabs ();
     }
     void WindowTabs()
     {
         if(GUI.Button(new Rect(0,0,32,32),"C", partSlotButton))
         {
             toolBarInt = 1;
         }
         if(GUI.Button(new Rect(32,0,32,32),"Co", partSlotButton))
         {
             toolBarInt = 2;
         }
         if(GUI.Button(new Rect(64,0,32,32),"P", partSlotButton))
         {
             toolBarInt = 3;
         }
         if(GUI.Button(new Rect(96,0,32,32),"FC", partSlotButton))
         {
             toolBarInt = 4;
         }
         if(GUI.Button(new Rect(128,0,32,32),"A", partSlotButton))
         {
             toolBarInt = 5;
         }
         if(GUI.Button(new Rect(160,0,32,32),"S", partSlotButton))
         {
             toolBarInt = 6;
         }
         if(GUI.Button(new Rect(192,0,32,32),"U", partSlotButton))
         {
             toolBarInt = 7;
         }
         if(GUI.Button(new Rect(224,0,32,32),"R", partSlotButton))
         {
             toolBarInt = 8;
         }
 
     }
     void ToolBarInt()
     {
         if (toolBarInt == 1)
         {
             cockpitWindowOpen = true;
             controlWindowOpen = false;
             propulsionWindowOpen = false;
             fuelContainersWindowOpen = false;
             aerodynamicsWindowOpen = false;
             structuralWindowOpen = false;
             utilitiesWindowOpen = false;
             researchWindowOpen = false;
         }
         if (toolBarInt == 2)
         {
             cockpitWindowOpen = false;
             controlWindowOpen = true;
             propulsionWindowOpen = false;
             fuelContainersWindowOpen = false;
             aerodynamicsWindowOpen = false;
             structuralWindowOpen = false;
             utilitiesWindowOpen = false;
             researchWindowOpen = false;
         }
         if (toolBarInt == 3)
         {
             cockpitWindowOpen = false;
             controlWindowOpen = false;
             propulsionWindowOpen = true;
             fuelContainersWindowOpen = false;
             aerodynamicsWindowOpen = false;
             structuralWindowOpen = false;
             utilitiesWindowOpen = false;
             researchWindowOpen = false;
         }
         if (toolBarInt == 4)
         {
             cockpitWindowOpen = false;
             controlWindowOpen = false;
             propulsionWindowOpen = false;
             fuelContainersWindowOpen = true;
             aerodynamicsWindowOpen = false;
             structuralWindowOpen = false;
             utilitiesWindowOpen = false;
             researchWindowOpen = false;
         }
         if (toolBarInt == 5)
         {
             cockpitWindowOpen = false;
             controlWindowOpen = false;
             propulsionWindowOpen = false;
             fuelContainersWindowOpen = false;
             aerodynamicsWindowOpen = true;
             structuralWindowOpen = false;
             utilitiesWindowOpen = false;
             researchWindowOpen = false;
         }
         if (toolBarInt == 6)
         {
             cockpitWindowOpen = false;
             controlWindowOpen = false;
             propulsionWindowOpen = false;
             fuelContainersWindowOpen = false;
             aerodynamicsWindowOpen = false;
             structuralWindowOpen = true;
             utilitiesWindowOpen = false;
             researchWindowOpen = false;
         }
         if (toolBarInt == 7)
         {
             cockpitWindowOpen = false;
             controlWindowOpen = false;
             propulsionWindowOpen = false;
             fuelContainersWindowOpen = false;
             aerodynamicsWindowOpen = false;
             structuralWindowOpen = false;
             utilitiesWindowOpen = true;
             researchWindowOpen = false;
         }
         if (toolBarInt == 8)
         {
             cockpitWindowOpen = false;
             controlWindowOpen = false;
             propulsionWindowOpen = false;
             fuelContainersWindowOpen = false;
             aerodynamicsWindowOpen = false;
             structuralWindowOpen = false;
             utilitiesWindowOpen = false;
             researchWindowOpen = true;
         }
 
     }
 }
 
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
Best Answer

Answer by ChristianLinnell · May 04, 2014 at 09:50 PM

Most probably, since the loop is starting, your gameobject doesn't have a PlacingParts component attached to it.

Add the following line before "placingparts.PlaceItem(cockpit[i]);":

 print("PlacingParts: " + placingparts != null);

If it prints "PlacingParts: false" that's your problem.

If it says true, check that cockpit[i] is actually set to something.

Comment
Add comment · Show 3 · 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 NutellaDaddy · May 05, 2014 at 12:41 AM 0
Share

It says true ,but cockpit[i] is an array that holds game objects that I publicly assign in the inspector and i is the number in the array. How could I assign this a value.

avatar image NutellaDaddy · May 05, 2014 at 12:58 AM 0
Share

I DIDN'T HAVE THE SCRIPT ON THE OBJECT. Thank you for showing me that line of code. It gave me some insight on debugging!

avatar image ChristianLinnell · May 05, 2014 at 01:07 AM 0
Share

No problem! Two things to keep in $$anonymous$$d:

  • ALWAYS check that the component exists before you try to assign it to a variable or so something with it if (whatever.GetComponent()){ ... }

  • if you're stuck, use print() everywhere!! Print the value of variables, print "in loop" "out of loop" "in if block" "in else block" flags all over the place.

avatar image
0

Answer by SeeSharp · May 04, 2014 at 09:53 PM

Hi,

Well, if you're asking what the problem is, the error tells you what it is. If you're asking for further explanation about what that means, then it means that

placingparts.PlaceItem(cockpit[i]);

was probably not assigned a value.

If you're looking for help on how to fix it, or find out what's wrong, I can only provide you with limited support as I do not know how you are setting that value.

You could try to see what's wrong by setting the value right before you're trying to use it. If you do that, and that gets rid of your error, then that means that you didn't set the value correctly/it wasn't set at all.

Just to recap:

 cockpit[i] = (whatever...);
 placingparts.PlaceItem(cockpit[i]);

Please feel free to ask for further assistance, and I'm sorry if I wasn't of any help.

Best of luck,

SeeSharp.

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

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

Related Questions

NullReferenceException: Object reference not set to an instance of an object PlacingParts.Update () (at Assets/Scripts/BuildingRocket/PlacingParts.cs:46) 1 Answer

I have never seen a null reference exception like this one! 1 Answer

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

Why doesen't the function get called the second itme? 1 Answer

How to make tab in editor near the top? 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