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 beau101023 · Jun 15, 2015 at 02:20 AM · errorobjectreferencesimulation

Object reference not set to an instance of an object.

I'm trying to create a basic simulation where cubes change color based on the color of adjacent cubes, but for some reason I keep getting this error:

NullReferenceException: Object reference not set to an instance of an object GOL_Determiner.Update () (at Assets/GOL_Determiner.cs:55)

I have verified that there is an object of the name I am trying to reference(created on Start).

 using UnityEngine;
 using System.Collections;
 
 public class GOL_Determiner : MonoBehaviour 
 {
     public bool alive = false;
     GameObject obj_west;
     GameObject obj_east;
     GameObject obj_south;
     GameObject obj_north;
     GameObject obj_northwest;
     GameObject obj_northeast;
     GameObject obj_southwest;
     GameObject obj_southeast;
     int neighbor_number = 0;
     public int upper_limit;
     public int lower_limit;
     public Transform initiator;
     // Update is called once per frame
     void Update () 
     {
         ColormapGenerator2D targetScript = initiator.GetComponent<ColormapGenerator2D>();
 
         obj_north = GameObject.Find ("(" + gameObject.transform.position.x + "," + gameObject.transform.position.y.Equals(gameObject.transform.position.y + 1) + ")");
         obj_northeast = GameObject.Find ("(" + gameObject.transform.position.x.Equals(gameObject.transform.position.x + 1) + "," + gameObject.transform.position.y.Equals(gameObject.transform.position.y + 1) + ")");
         obj_northwest = GameObject.Find ("(" + gameObject.transform.position.x.Equals(gameObject.transform.position.x - 1) + "," + gameObject.transform.position.y.Equals(gameObject.transform.position.y + 1) + ")");
         obj_south = GameObject.Find ("(" + gameObject.transform.position.x + "," + gameObject.transform.position.y.Equals(gameObject.transform.position.y - 1) + ")");
         obj_southwest = GameObject.Find ("(" + gameObject.transform.position.x.Equals (gameObject.transform.position.x - 1) + "," + gameObject.transform.position.y.Equals(gameObject.transform.position.y - 1) + ")");
         obj_southeast = GameObject.Find ("(" + gameObject.transform.position.x.Equals (gameObject.transform.position.x + 1) + "," + gameObject.transform.position.y.Equals(gameObject.transform.position.y - 1) + ")");
         obj_west = GameObject.Find ("(" + gameObject.transform.position.x.Equals(gameObject.transform.position.x - 1) + "," + gameObject.transform.position.y + ")");
         obj_east = GameObject.Find ("(" + gameObject.transform.position.x.Equals(gameObject.transform.position.x + 1) + "," + gameObject.transform.position.y + ")");
         if (gameObject.transform.position.y != targetScript.height) 
         {
             if (obj_north.renderer.material.color == Color.green) 
             {
                 neighbor_number = neighbor_number + 1;
             }
         }
         if (gameObject.transform.position.y != targetScript.height && gameObject.transform.position.x != targetScript.length) 
         {
             if (obj_northeast.renderer.material.color == Color.green) 
             {
                     neighbor_number = neighbor_number + 1;
             }
         }
         if (gameObject.transform.position.y != targetScript.height && gameObject.transform.position.x != 0) 
         {
             if (obj_northwest.renderer.material.color == Color.green) 
             {
                     neighbor_number = neighbor_number + 1;
             }
         }
         if (gameObject.transform.position.y != 0) 
         {
             if (obj_south.renderer.material.color == Color.green)
             {
                 neighbor_number = neighbor_number + 1;
             }
         }
         if (gameObject.transform.position.x != targetScript.length)
         {
             if (obj_east.renderer.material.color == Color.green) 
             {
                 neighbor_number = neighbor_number + 1;
             }
         }
         if (gameObject.transform.position.x != 0)
         {
             if (obj_west.renderer.material.color == Color.green) 
             {
                 neighbor_number = neighbor_number + 1;
             }
         }
         if (gameObject.transform.position.y != 0 && gameObject.transform.position.x != targetScript.length) 
         {
             if (obj_southeast.renderer.material.color == Color.green) 
             {
                 neighbor_number = neighbor_number + 1;
             }
         }
         if (gameObject.transform.position.y != 0 && gameObject.transform.position.x != 0) 
         {
             if (obj_southwest.renderer.material.color == Color.green) 
             {
                 neighbor_number = neighbor_number + 1;
             }
         }
         if (neighbor_number > upper_limit) 
         {
             alive = false;
         }
         if (neighbor_number < lower_limit) 
         {
             alive = false;
         }
         if (neighbor_number > lower_limit && neighbor_number < upper_limit) 
         {
             alive = true;
         }
 
         if (alive == true) 
         {
             gameObject.renderer.material.color = Color.green;
         }
         if (alive == false) 
         {
             gameObject.renderer.material.color = Color.gray;
         }
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Addyarb · Jun 15, 2015 at 03:38 AM

To debug, first lets make sure there is indeed an object with that name existing. Not doubting you, just covering all the bases.

In the start function, put:

 void Start(){
      obj_south = GameObject.Find("obj_south");
      if(obj_south)
           Debug.Log(obj_south + " does actually exist");
      else
           Debug.Log("No object found for obj_south");
 }


Next,

Lets check that there is a renderer on the object.

For this, put an extra line in your start function below the bit we just typed, that looks like this.

 if(obj_south.GetComponent<Renderer>())
 Debug.Log("obj_south has a renderer");
 else
 Debug.Log("No renderer found for obj_south");

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 beau101023 · Jun 15, 2015 at 03:26 PM 0
Share

Have confirmed that no object exists at script run time. Going to try a delay of 1 second~ before attempting to call the objects.

avatar image beau101023 · Jun 15, 2015 at 03:41 PM 0
Share

Test successful, it appears the script was executing before the objects were instantiated. Thanks for the help.

Edit: Never$$anonymous$$d, delay of one second and still getting object errors.

NullReferenceException: Object reference not set to an instance of an object GOL_Deter$$anonymous$$er.Update () (at Assets/GOL_Deter$$anonymous$$er.cs:55)

Edit2: Tested further with a delay of ten seconds, errors start at the execution of script regardless of delay.

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

22 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

Related Questions

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

Script decides not to run anymore 1 Answer

Object reference not set to an instance of an object. 2 Answers

Object reference not set to... 1 Answer

Object Reference not set to an Instance 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