Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Raja-Unity · Aug 10, 2017 at 12:35 AM · scripting problemerrorobject referenceunknown

For some reason Object reference not set to instance of an object

I got an error in the console saying object reference not set to instance of an object. The error in the console is not very clear on telling me which thing is not set to an instance of object. It just tells me the line that causes the error. I can't figure out what's wrong with my code.

Here is the error:

NullReferenceException: Object reference not set to an instance of an object LockWallScript.OnCollisionEnter2D (UnityEngine.Collision2D col) (at Assets/EditorAssets/Scripts/LockWallScript.cs:7)

and here is my code simplified:

LockWallScript:

 public class LockWallScript : MonoBehaviour {
 
     public bool locked = true;
 
     void OnCollisionEnter2D (Collision2D col) {
         if (col.gameObject == GameObject.Find("Player")) {
                 Game.game.findScene (SceneManager.GetActiveScene ().name).unlockedLockWalls.Add (transform.GetSiblingIndex ());
         }
     }
 
     void Start () {
         locked = true;
     }
 }

Game class:

 [System.Serializable]
 public class Game {
 
     public static Game game;
 
     public List<SceneLayout> scenes;
 
     public Game () {
         scenes = new List<SceneLayout>();
     }
 
     public SceneLayout findScene (string nm) {
         bool sceneAvailable = false;
         SceneLayout scene = null;
 
         foreach (SceneLayout i in scenes) {
             if (i.name == nm) {
                 sceneAvailable = true;
                 scene = i;
             }
         }
 
         if (sceneAvailable) {
             return scene;
         } else if (!sceneAvailable) {
             return null;
         }
 
         return null;
     }
 }

SceneLayout class:

 [System.Serializable]
 public class SceneLayout {
     public string name;
 
     public List<int> unlockedLockWalls;
 
     public SceneLayout (string nm) {
         name = nm;
     }
 }

GameplayControlScript:

 public class GameplayControlScript : MonoBehaviour {
 
     public static GameplayControlScript gameplay;
 
     void Awake () {
         gameplay = this;
         Game.game = new Game ();
         Game.game.scenes.Add(new SceneLayout(SceneManager.GetActiveScene ().name))
     }
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }

Can anyone tell me what is causing the error?

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 Raja-Unity · Aug 10, 2017 at 06:24 PM

I just found out what was causing the error! When I create a new SceneLayout() I just have to set the unlockedLockWalls to a new List()

Here is the SceneLayout class:

 [System.Serializable]
  public class SceneLayout {
      public string name;
  
      public List<int> unlockedLockWalls;
  
      public SceneLayout (string nm) {
          name = nm;
          unlockedLockWalls = new List<int>();
      }
  }
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 unidad2pete · Aug 10, 2017 at 12:55 AM

I think your LockWallScript dont have any reference to your game class.

  public class LockWallScript : MonoBehaviour {
  
      public bool locked = true;
      public Game game;
  
      void OnCollisionEnter2D (Collision2D col) {
          if (col.gameObject == GameObject.Find("Player")) {
             if(game != null)
             {
                    if (game.worldKeys > 0 && locked) {
                    game.findScene (SceneManager.GetActiveScene 
                     ().name).unlockedLockWalls.Add (transform.GetSiblingIndex ());
                     }
            }
          }
      }
  
      void Start () {
          locked = true;
      }
  }

On Inspector, drag Object with script Game to the new field.

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 Raja-Unity · Aug 10, 2017 at 01:17 AM 0
Share

The Game class is not supposed to be a $$anonymous$$onoBehaviour. When I added the if statement, the error was still in the console

avatar image unidad2pete Raja-Unity · Aug 10, 2017 at 01:30 AM 0
Share

Oh, thats right , my fault :S

But there are something on code inside of OnCollisionEnter2D code not found on your reference.

What is the code exactly on line 13 of your code?

Try add new line to check if player its found

 public class LockWallScript : $$anonymous$$onoBehaviour {
  
      public bool locked = true;
  
      void OnCollisionEnter2D (Collision2D col) {
          if (col.gameObject == GameObject.Find("Player")) {
                  print("Player found");      
                  print(Game.game.findScene (Scene$$anonymous$$anager.GetActiveScene ().name);
                  Game.game.findScene (Scene$$anonymous$$anager.GetActiveScene ().name).unlockedLockWalls.Add (transform.GetSiblingIndex ());
          }
      }
  
      void Start () {
          locked = true;
      }
  }

try run this code, and tell me if any thing is printed on console please.

avatar image Raja-Unity unidad2pete · Aug 10, 2017 at 01:37 AM 0
Share

I simplified all the scripts shown so the actual line that has the error is line 7. When I printed to the console it all worked fine but the error is still there.

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

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

Related Questions

I have an error saying its not set to an instance of an object althoug i have set it to an object. Please Help ?? 1 Answer

Help with editing array from inside functions for PID controller (C#)[Fixed] 1 Answer

Health.cs(331,22): error CS1624: The body of `Opsive.ThirdPersonController.Health.DieLocal(UnityEngine.Vector3, UnityEngine.Vector3)' cannot be an iterator block because `void' is not an iterator interface type 1 Answer

Help with an error. 1 Answer

"Object reference not set to an instance of an object" Error 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