• 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 maroonswordsman · Aug 13, 2016 at 02:58 PM · errorgetcomponentnull reference exceptiongameobject.findfindgameobjectwithtag

Unity3d Script does not work anymore

I was working on a simple tank game. When the script I was using before suddenly stop working. I keep receiving the NullReferenceException: Object reference not set to an instance of an object error. I searched many questions online but none of them are similar to mine. The game object component that the unity engine claims is null is the parent of the script with the error. Moreover the script being referenced is what controls my game object which it does fine. Everything worked fine until I restarted my computer. I have gone over my script over and over and nothing have been changed but I keep getting the error. Here is the script with the error:

 private GameObject i; 
 
 // Use this for initialization
 void Awake () {
     i = GameObject.FindGameObjectWithTag("WallBoy");
 }
 
 void Start () {
     i = GameObject.FindGameObjectWithTag("WallBoy");
 }
 
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.CompareTag ("Walls")) {
         i.GetComponent<TW>().x += 1;
     }
 }
 }
Comment
Add comment · Show 4
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 Zoogyburger · Aug 13, 2016 at 07:56 PM 0
Share

When I tried out your script, there was an error with the line:

 i.GetComponent<TW>().x += 1;

The namespace 'TW' couldn't be found. So what exacly is TW?

Also, I don't think you need to find your gameobject in both Start and Awake.

avatar image maroonswordsman Zoogyburger · Aug 14, 2016 at 01:47 PM 0
Share

TW is a script attached to the parent GameObject that controls the main player, which it does fine. The main player has the tag "WallBoy". The script worked fine when I first made it. However after I shut down my computer and restarted it, I began to get the null reference error. I have also checked the scripts and gameObjects many times and nothing seems to have changed.

avatar image Zoogyburger · Aug 14, 2016 at 07:52 PM 0
Share

Very strange... I'm not sure what's going on. Sometimes, the 'NullReferenceException' is caused by something not being assigned in the inspector. Is the error always there or does it appear when you hits the wall?

avatar image Addyarb · Aug 14, 2016 at 08:00 PM 0
Share

Replace your code with this, and let me know what it says.

     private GameObject i;
 
     void Start()
     {
         GameObject[] allWallBoys = GameObject.FindGameObjectsWithTag("WallBoy");
 
         if (allWallBoys.Length > 1) Debug.Log("There are multiple instances of Wall Boy in this scene");
 
         if (GameObject.FindWithTag("WallBoy") != null) i = GameObject.FindWithTag("WallBoy");
         else Debug.Log("Unable to find Wall Boy");
 
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Walls"))
         {
             if (i.GetComponent != null) i.GetComponent<TW>().x += 1;
             else Debug.Log("No TW component attached to i");
         }
 
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by meapps · Oct 19, 2016 at 09:17 AM

Broken Tags, just use an other tag that will solve it. Somehow updates seems to break stuff.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error That Doesn't Affect My Game Is Spammed Every Frame. 1 Answer

GetComponent and GameObject.Find stopped working 0 Answers

Not sure what this error is telling/asking me to do? 1 Answer

GetComponnent null reference C# 1 Answer

Why can't monodevelop find this public method in my component? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges