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 /
  • Help Room /
avatar image
0
Question by CommonTroll · Nov 07, 2016 at 10:57 PM · c#variablesmethodmethodsscope

Declared variable being returned as null in a method?

I'm stumped as to what the solution to this problem is, and I've been looking for an answer for days to no avail. The problem is that when one particular method tries to access a public gameObject that's been assigned in the editor, it returns as null. If I check anywhere else in the script, it returns the object just fine. It's just the one method that returns it as null. Here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class HackableController : MonoBehaviour {
 
     public bool readyToHack;
     public bool puzzleActive;   
     public GameObject hackPuzzle;
     public GameObject hackObj;
     private GameObject playerOne;
     private Player playerController;
     private PuzzleController puzzleController;
 
     void Start()
     {
         playerOne = GameObject.FindGameObjectWithTag("PlayerOne");
         playerController = playerOne.GetComponent<Player>();
         puzzleController = hackPuzzle.GetComponent<PuzzleController>();
     }
 
     void Update()
     {
         if (readyToHack && !puzzleActive)
         {
             if (Input.GetKeyDown(KeyCode.E))
             {
                 puzzleActive = true;
                 playerController.canMove = false;
                 hackPuzzle.gameObject.SetActive(true);
                 puzzleController.StartObstacleSpawn();
             }
         }
         else if (readyToHack && puzzleActive)
         {
             if (Input.GetKeyDown(KeyCode.E))
             {
                 puzzleActive = false;
                 playerController.canMove = true;
                 puzzleController.DestroyPuzzleObstacles();
                 hackPuzzle.gameObject.SetActive(false);
             }
         }
     }
 
     public void OpenHackableObject()
     {
         Debug.Log("Door Object: " + hackObj);
         hackObj.gameObject.SetActive(false);
     }    
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "PlayerOne")
         {
             readyToHack = true;
         }
     }
 
     void OnTriggerExit2D(Collider2D other)
     {
         if (other.tag == "PlayerOne")
         {
             readyToHack = false;
         }
     }
 }

The problem occurs in the "OpenHackableObject" method. The method is called from another script, if that makes a difference, but passes in no variables of its own. There is also no NullReferenceException, it simply doesn't work.

Comment
Add comment · Show 14
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 JScotty · Nov 08, 2016 at 12:40 PM 2
Share

Can you show how you call it in your other class, because the only thing I can think of is that you are calling an new 'empty' instance of your HackableController class.

So make sure in your other script you are referring to this exact class.

avatar image CommonTroll JScotty · Nov 08, 2016 at 12:42 PM 0
Share

The method is called just fine, it just can't access the hackObj variable for some reason.

avatar image JScotty JScotty · Nov 08, 2016 at 12:58 PM 1
Share

But that can't be,

If you get a NULL output because it's been called from another script, means the script has an empty reference of HackableController script.

avatar image JScotty JScotty · Nov 08, 2016 at 01:04 PM 1
Share

But if you call OpenHackableObject inside the OnTriggerEnter2D it output is still null?

so like

  public void OpenHackableObject()
  {
      Debug.Log("Door Object: " + hackObj); //output null?
      hackObj.gameObject.SetActive(false);
  }    
  
  void OnTriggerEnter2D(Collider2D other)
  {
      Debug.log("[ TEST ] "+hackObj); //output hackobj?
      OpenHackableObject();
      if (other.tag == "PlayerOne")
      {
          readyToHack = true;
      }
  }


Show more comments
avatar image hexagonius · Nov 10, 2016 at 07:04 PM 0
Share

The nearest idea that I have about this is that the PuzzleController does not reference a GameObject in the scene (hierarchy) but a prefab from the project view that is not instantiated. This would at least explain why nothing happens but there's also no NullReference, because the wired prefab might have correctly set references on itself but is not part of the current game.
You named those variable ...prefab which made me curious.
Other than that, set breakpoints in $$anonymous$$onoDevelop and see where you're getting.

avatar image CommonTroll hexagonius · Nov 11, 2016 at 11:44 AM 0
Share

It gets to the OpenHackableObject method in the HackableController, which is where it runs into the issue of the door being null, despite being set in the editor.

avatar image Graphics_Dev · Nov 11, 2016 at 01:54 PM 0
Share

Is this a 2D game? Could you show a screenshot of the inspector when the game is running?

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

re historic GUI system 1 Answer

Method Skipping Inputs 2 Answers

How to access a variable from another C# script in Unity 1 Answer

I can't seem to have access to Tilemap methods, what am I doing wrong ? 0 Answers

List.FindIndex 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