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

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 CS0426 expression denotes a type' where a variable' value' or method group' was expected Rigidbody.velocity problem? 1 Answer

Gameobject reference disappears at start 1 Answer

NullReferenceException Error 0 Answers

Problem Destroying Component 0 Answers

why i am getting this errorr.... 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