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 /
This question was closed May 08, 2014 at 08:26 PM by perchik for the following reason:

The question is answered in the comments

avatar image
0
Question by Forte259 · May 08, 2014 at 05:15 PM · gameobjectnullreferenceexceptiontagsfind

Find with tag throwing an Null exception

Hey guys, I come up with a problem and I tried to resolve it by a numerous ways with no luck that I'm starting to get a bit crazy lol, because I think its a stupid problem.

So my problem is that I'm trying to get a GameObject through FindWithTag, but unity just keekps throwing a Null reference exception! I dont know why, I have the prefab tagged in the editor and have no mispelling errors, and I tried diferent names of tag with no luck aswell.

Here's me code (part of it):

 public class Item: MonoBehaviour {
 
     public GameObject Player;
 
     public bool CanDo = false;
 
     void Awake () {
 
         if (Player == null) {
 
             Player = GameObject.FindWithTag ("PlayerOwner");
         }
     }
 
     void Update () {
 
         if (Vector3.Distance (Player.transform.position, transform.position) < 5) {
 
             CanDo = true;
         }

Hope you guys can help me! Thanks in advance!

Comment
Add comment · Show 3
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 robertbu · May 08, 2014 at 05:16 PM 0
Share

In order to find an object (GameObject.Find() or GameObject.FindWithTag()), the object must exist in the scene (i.e. cannot just be a prefab), and it must be active. Any change the 'PlayerOwner' object is not created at this point in the game? Does moving this code to Start() fix the problem?

avatar image Forte259 · May 08, 2014 at 05:49 PM 0
Share

No, Start doesnt solve the problem, and yes the playes is instantiated when the game plays, thats may be the problem, so is there any way arround? Thx

avatar image robertbu · May 08, 2014 at 05:57 PM 1
Share

If ti$$anonymous$$g is your problem, you can work with script execution order and the use of Awake() vs. Start() to solve the problem. You could also delay just a bit initializing 'Player', but if you do, you'd have to also check for null in Update():

 IEnumerator Start() {
     yield return new WaitForSeconds(0.2);
 
      if (Player == null) {
          Player = GameObject.FindWithTag ("PlayerOwner");
        }
 }

And in Update():

 if (Player == null) return;

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Forte259 · May 08, 2014 at 08:25 PM

I've fixed it, the problem was indeed the order of instatiating the objects, so I changed it up a little and it works fine rigth now. Thank you for the help!

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

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Best method to manage multiple tags 2 Answers

Find a shader not in the scene 1 Answer

Can't find gameobjects being spawned getting a nullreferenceexception 1 Answer

How to use if statement properly 1 Answer

NullReferenceException 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