• 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 uanmanarmy · Jul 21, 2014 at 12:55 PM · instantiatereferenceinstance

Object Reference not set to an instance of an object

I saw that My game is not working properly. So I build it in Development Build with Script Debugger and look what I have found )) .

alt text

Here are my scripts

 public class MenuScript : MonoBehaviour {
 
     public int newLevel  ;
     public int numbersOfLevels;
     public bool LevelGo = false;
     public bool StartGame = false;
     public GameObject myObject;
     XMLParserScript menuSriptObj ;
 
     public GameObject myObjet;
     AutoPositionItems autoObject;
 
     public GameObject gemObject;
     Board boardObject;
     void Start()
     {
         menuSriptObj = myObject.GetComponent<XMLParserScript> ();
         autoObject = myObjet.GetComponent<AutoPositionItems> ();
         boardObject = gemObject.GetComponent<Board> ();
     }
     void OnGUI()
     {
         if(GUI.Button(new Rect(20,20,120,120), "New Level"))
         {    
             if(!LevelGo)
                     boardObject.CreateGemsGrid();
 
             if(LevelGo){
             //boardObject.DeleteGemsGrid();
             //boardObject.CreateGemsGrid();
                 
             }
             LevelGo = true;

//HERE IS THE LINE 37 //HERE IS THE LINE 37 //HERE IS THE LINE 37 Down bellow

             menuSriptObj.LevelList();
             autoObject.DeleteItems();
             autoObject.CreateItems();
 
             // % will make sure to go from beginning when newlevel == numbersOfLevels - 1
             newLevel = (newLevel+1) % numbersOfLevels;
         }
     }
 }


And the second one.

 using UnityEngine;
 using System.Collections;
 using System.Xml;
 using System.IO;
 using System.Xml.Serialization;
 using System.Collections.Generic;
 
 public class XMLParserScript : MonoBehaviour {
 
     string path = "Assets/Resources/Xml/Level1.xml";
 
     public MenuScript objectScript;
     public GameObject myObject;
     LevelsDirectory XmlData;
     public bool NewLevel = false;
 
     //[HideInInspector]
     public int ItemsNumber;
     public string ItemsColor;
     void Start()
     {
         objectScript = myObject.GetComponent <MenuScript>();
         XmlSerializer deserializer = new XmlSerializer (typeof(LevelsDirectory));
         TextReader reader = new StreamReader (path);
         
         // deserializer.Deseralize function is used to deserializethe XML data which is there in XML file
         object obj = deserializer.Deserialize(reader);
         XmlData = (LevelsDirectory)obj;
         reader.Close ();
         LevelList ();
     //    ItemsNumber = XmlData.levelList[0].ItemsNumber;
     }
 
 
     public void LevelList()
     {
         switch(objectScript.newLevel)
             {
             case 0:


//HERE IS THE LINE 40 //HERE IS THE LINE 40 //HERE IS THE LINE 40 DownBellow

             ItemsNumber = XmlData.levelList[0].ItemsNumber;
             ItemsColor = XmlData.levelList[0].ItemsColor.ToUpper();
             break;
 
             case 1:
             ItemsNumber = XmlData.levelList[1].ItemsNumber;
             ItemsColor = XmlData.levelList[1].ItemsColor.ToUpper();
             break;
                 
             case 2:
             ItemsNumber = XmlData.levelList[2].ItemsNumber;
             ItemsColor = XmlData.levelList[2].ItemsColor.ToUpper();
             break;
 
         default:
                 break;
                 
             }
     }
 }
     public class LevelsDirectory
     {
         [XmlElement("Level")]
         public List<Level> levelList = new List<Level> ();
     }
 
     public class Level
     {
         public int LevelNumber{ get; set; }
         public int ItemsNumber{ get; set; }
         public string ItemsColor { get; set; }
     }


I need some help from you because I don't really find where is this instance of an object ))

безымянный.png (30.2 kB)
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

3 Replies

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

Answer by uanmanarmy · Jul 22, 2014 at 09:10 AM

Resources - Read it carefully

The problem was that the way I accessed the Xml file was wrong.

At build stage, only the assets that are in the Resources file will be compiled. That's why I had to Load the Xml File as

 TextAsset itemTextFile = Resources.Load ("Xml/Level1") as TextAsset;

and than to parse the itemTexFile.txt

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
1

Answer by drodrii · Jul 21, 2014 at 02:59 PM

What caleb said and also your object is not pointed to anything, unless you dragged it on the inspector.

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 calebheale · Jul 21, 2014 at 01:34 PM

In the script shown, levelList is not populated.

 public List<Level> levelList = new List<Level>():

is giving you an empty list so trying to access an index without a value could give you null.

If you do think that you are populating the list somewhere, I would start by checking if it is actually getting values.

You could do a simple

 Debug.Log(levelList.Count)

to see how many (if any) values it contains.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

A node in a childnode? 1 Answer

Instantiating on Input 1 Answer

Unity Network Instantiate Issue 0 Answers

Instantiated GameObject being deleted when destroying original 1 Answer

Show/hide object in C# 2 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